Home > DeveloperSection > Forums > How do I calculate someone's age
Jeet Verma
Jeet Verma

Total Post:110

Points:776
Posted on    May-29-2013 2:14 AM

 C# C# 
Ratings:


 1 Reply(s)
 701  View(s)
Rate this:
Hi Expert!

Given a DateTime representing a person's birthday, how do I calculate their age?

Thanks in advance. 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-29-2013 9:04 AM

Hi Jeet!

For some reason Jeff's code didn't seem simple enough. To me this seems simpler and easier to understand:

DateTime today = DateTime.Today;
int age = today.Year - bday.Year;
if (bday > today.AddYears(-age)) age--;

Don't want to miss updates? Please click the below button!

Follow MindStick