467,188 Members | 1,406 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,188 developers. It's quick & easy.

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" <ve******@comcast.net> 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" <ve******@comcast.net> 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" <ve******@comcast.net> 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.

Similar topics

1 post views Thread by Joe Bongiardina | last post: by
1 post views Thread by jlm | last post: by
reply views Thread by robin9876@hotmail.com | last post: by
5 posts views Thread by sugaray | last post: by
25 posts views Thread by Umesh | last post: by
1 post views Thread by laredotornado@zipmail.com | last post: by
4 posts views Thread by =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.