467,188 Members | 1,406 Online

# Calculating Age

 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 Nov 15 '05 #1
• viewed: 12659
Share:
3 Replies
 Maybe something like this? http://temp.codefanatic.com/usenet/age.txt "Ron Vecchi" wrote in message news:uf**************@TK2MSFTNGP12.phx.gbl... 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 Nov 15 '05 #2
 DateTime.Now.Year - BirthDate.Year - (BirthDate.Month > DateTime.Now.Month ? 1 : (BirthDate.Month != DateTime.Now.Month) ? 0 : (BirthDate.Day > DateTime.Now.Day) ? 1 : 0); "Ron Vecchi" wrote in message news:uf**************@TK2MSFTNGP12.phx.gbl... 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 Nov 15 '05 #3
 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.") "Ron Vecchi" wrote in message news:uf**************@TK2MSFTNGP12.phx.gbl... 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 Nov 15 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion.