Calculate Someone's Age Based On Birthday In Asp.Net (C#)
Here we consider how to Calculate Age From Birthday.
For that we have to compare the Birth Date with respect to the Current Date.
Suppose One person's Birthday is on 12/2/1998.
And we wants to calculate the age of this person.
First of all, get the Year of the Current date - Current Year.
Second get the year of the Birth date. - Birth Year.
Then just subtract the Current Year from Birth Year.
Assign the difference to an integer variable age.
Now we can do the following comparison for getting the exact age.
if (birthDate > todayDate.AddYears(-age))
After Comparison, if birthdate is greater, then subtract one from the current age.
The full code is as follows.
DateTime birthDate =
Convert.ToDateTime("12/2/1998");
DateTime todayDate = DateTime.Today;
int age = todayDate.Year - birthDate.Year;
if (birthDate > todayDate.AddYears(-age))
age--;