Duplicate
I have a datetime
variable that represents the date of birth of a user.
How can I get the age in years from this?
Update I want a precise birthday, so 30.45 years or something.
I have a datetime
variable that represents the date of birth of a user.
How can I get the age in years from this?
Update I want a precise birthday, so 30.45 years or something.
Try the following (assuming the date of birth is stored in dtDOB
):
public int getAgeInYears {
TimeSpan tsAge = DateTime.Now.Subtract(dtDOB);
return new DateTime(tsAge.Ticks).Year - 1;
}
Stolen from the answer to Jeff's question:
DateTime now = DateTime.Now;
int age = now.Year - bday.Year;
if (now < bday.AddYears(age)) age--;