Age Calculator Using C#

In this tutorial, we will going to create a program that can calculate your age using C# as the programming language. So, now let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Calculate age. 2. Next, add one DateTimePicker named DateTimePicker1 to have input of our dates. Add one TextBox named TextBox1 that will serve as the display of our age. Add also a Button named Button1 so that we can use this calculating our age. Design your interface like this one below: design 3. Put this code for the Button1_Click. This will trigger to calculate your age. We will initialized yr variable as integer to have the year difference between the present year and the year you were born as we code this syntax:
  1. int yr = (int) (DateAndTime.DateDiff(DateInterval.Year, DateTimePicker1.Value, DateTime.Now, (Microsoft.VisualBasic.FirstDayOfWeek) Microsoft.VisualBasic.FirstDayOfWeek.Sunday, (Microsoft.VisualBasic.FirstWeekOfYear) Microsoft.VisualBasic.FirstWeekOfYear.Jan1));
We initialized yr to have this month difference between the present month and the month you were born and we find its remainder as code Mod 12 because it has 12 Months as we code this syntax:
  1. int month = System.Convert.ToInt32(DateAndTime.DateDiff(DateInterval.Month, DateTimePicker1.Value, DateTime.Now, (Microsoft.VisualBasic.FirstDayOfWeek) Microsoft.VisualBasic.FirstDayOfWeek.Sunday, (Microsoft.VisualBasic.FirstWeekOfYear) Microsoft.VisualBasic.FirstWeekOfYear.Jan1) % 12);
Next, we put this code below to display your age in the Textbox.
  1. TextBox1.Text = yr.ToString() + " Years, " + month.ToString() + " Months ";
Full source code:
  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using Microsoft.VisualBasic;
  7. using System.Data;
  8. using System.Collections.Generic;
  9.  
  10.  
  11.  
  12. namespace Calculate_Age
  13. {
  14.         public partial class Form1
  15.         {
  16.                 public Form1()
  17.                 {
  18.                         InitializeComponent();
  19.                        
  20.  
  21.                 }
  22.                
  23.                
  24.                 public void Button1_Click(System.Object sender, System.EventArgs e)
  25.                 {
  26.                         int yr = (int) (DateAndTime.DateDiff(DateInterval.Year, DateTimePicker1.Value, DateTime.Now, (Microsoft.VisualBasic.FirstDayOfWeek) Microsoft.VisualBasic.FirstDayOfWeek.Sunday, (Microsoft.VisualBasic.FirstWeekOfYear) Microsoft.VisualBasic.FirstWeekOfYear.Jan1));
  27.                         int month = System.Convert.ToInt32(DateAndTime.DateDiff(DateInterval.Month, DateTimePicker1.Value, DateTime.Now, (Microsoft.VisualBasic.FirstDayOfWeek) Microsoft.VisualBasic.FirstDayOfWeek.Sunday, (Microsoft.VisualBasic.FirstWeekOfYear) Microsoft.VisualBasic.FirstWeekOfYear.Jan1) % 12);
  28.                         //   Dim day As Integer = DateDiff(DateInterval.Day, DateTimePicker1.Value, Now) Mod 30 - 10
  29.                         TextBox1.Text = yr.ToString() + " Years, " + month.ToString() + " Months ";
  30.                 }
  31.         }
  32.        
  33. }
Output: output Download the source code below and try it! :) Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment