How to Calculate Age from DOB in C#

In this article, we will learn how to calculate age from your DOB(Date of Birth) till the present date in C# Windows Application. It could simply be called an age calculator.

Age calculator in C# Windows Application

For making the age calculator we will take inputs from the user in the Text box we will take the input of the user’s DOB and subtract it from the current date, after all the calculation that is applied on it it will give us the output in total years, months and days that are passed till now from the DOB. For doing this operation we have to follow some simple steps these are:

Step 1:

Open your visual studio > click on File menu > Click on Create New Project > from left panel in visual C# select Windows Form Application > give the name to the project as Age calculator > Click on ok button.

Step 2:

The first form will be display after the above step it will show the name as Form1 so we will go to the property window of the form and change the text as Age Calculator.

Step 3:

Now we have to design the application so we will take here some controls from the toolbox those are 4 Labels, 4 Text boxes and 2 Buttons. Name the labels respectively DOB, Years, Months, and Days. And give the button’s name as Calculate Age and Exit. Design the page as you want.

Step 4:

Now write the below code in the Age calculator.cs file.

C# code to find Age from date

```private void btnCalculateAge_Click(object sender, EventArgs e) {
DateTime dateOfBirth; DateTime.TryParse(txtDOB.Text, out dateOfBirth);
DateTime currentDate = DateTime.Now;
TimeSpan difference = currentDate.Subtract(dateOfBirth);
DateTime age = DateTime.MinValue + difference;
int ageInYears = age.Year - 1;
int ageInMonths = age.Month - 1;
int ageInDays = age.Day - 1;
txtAgeInYear.Text = (ageInYears).ToString();
txtAgeInMonth.Text = (ageInMonths).ToString();
txtAgeInDays.Text = (ageInDays).ToString();
}
private void btnCalculateAge_MouseMove(object sender, MouseEventArgs e) {
toolTip1.SetToolTip(this.btnCalculateAge, "Enter to Calculate Your Age");
}
private void txtDOB_MouseHover(object sender, EventArgs e) {
}
private void btnExit_Click(object sender, EventArgs e) {
Application.Exit();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
DialogResult result;
result = MessageBox.Show("Do you realy want to close", "Confirmation", MessageBoxButtons.YesNo); if (result == DialogResult.No)
{
e.Cancel = true;
}
}```

Step 5:

Now Run the application and enter the input in the text box of DOB then Click on button Calculate Age. It will show the output in the years, months, and days after calculation.

For example- if we have entered our date of birth as 21/06/1998 and if the present date is 30/09/2021 in our system so after the Calculation the result will be like as shown below.

Output screen:

We can clearly see that it is showing our age after Calculation 23 years, 3 months, and 9 days.

if you click on the Exit button it will show the message box “Do you really want to close” if we click on yes it will close the window of the form else if we click on No it will not close the window.

So in this way, we can easily create a simple Age Calculator in C# Windows Application.