How To Calculate Age in C#
Friends,
Another post that is used in almost every application built where one need to calculate the age of a person using C# when his/her Date Of Birth is given. This post will explain how can we calculate age in C# when the user enters his/her Date Of Birth.
static void CalculateAndPrintAge(DateTime birthday)
{
DateTime curDate = DateTime.Today;
// check to ensure that we have a valid birthdate prior to today
if(birthday < curDate)
{
int age = curDate.Year - birthday.Year;
if (birthday.Month > curDate.Month)
{
age--;
}
Console.WriteLine("The person's age is: {0}", age);
}
}
Hope the post helps you . Keep learning and sharing.