Calculating Age

  • Thread starter Thread starter Ron Vecchi
  • Start date Start date
R

Ron Vecchi

I need to calculate the age of a person based on a DateTime BirthDate



I was thinking
TimeSpan ts = DateTime.Now - BirthDate;

//I can get the days but not years.
// I could check each year from their year of birth, count the days in the
year and compare with the days returned from timespan

but isn't there a easy way to check the difference in years?


Thanks,
Ron Vecchi
 
DateTime.Now.Year - BirthDate.Year -

(BirthDate.Month > DateTime.Now.Month ? 1

: (BirthDate.Month != DateTime.Now.Month) ? 0

: (BirthDate.Day > DateTime.Now.Day) ? 1 : 0);
 
The problem with calculating the difference in years is that the legth of a
year is not a definite timespan (actual number of days in a year depends on
its number). Same problem is applicable to months... (As the documentation
states : "Due to a varying number of days in months and years, the longest
unit of time that is used by TimeSpan is the day.")
 
Back
Top