Given a DateTime
representing a person\'s birthday, how do I calculate their age in years?
I've made one small change to Mark Soen's answer: I've rewriten the third line so that the expression can be parsed a bit more easily.
public int AgeInYears(DateTime bday)
{
DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday.AddYears(age) > now)
age--;
return age;
}
I've also made it into a function for the sake of clarity.