How to calculate age using TimeSpan w/ .NET CF?

G

Guest

Can someone pls point me to or recommend the easiest way to calculate
someone´s age using the TimeSpan object, in .NET CF?

Isn´t there a simple way to use the TimeSpan object to calculate the elapsed
time and somehow convert the days to years w/out using the VB DLL?

Thanks.
 
J

Jon Skeet [C# MVP]

charliewest said:
Can someone pls point me to or recommend the easiest way to calculate
someone?s age using the TimeSpan object, in .NET CF?

Isn?t there a simple way to use the TimeSpan object to calculate the elapsed
time and somehow convert the days to years w/out using the VB DLL?

What's wrong with the normal non-CF way of taking today's date (as a
DateTime) and their birthdate (likewise), then subtracting one from
another to get a TimeSpan?
 
G

Guest

hi
DateTime d1=DateTime.Now ;
DateTime d2 = new DateTime(1978,4,5);
TimeSpan difference = d1.Subtract(d2);

difference.TotalDays/365.25).ToString() will give you the years diffrence

regards
Ansil
 
G

Guest

There´s nothing wrong w/ the normal way if you know how ;-). Unfortunately, i
have been pampered by things like DateDiff. Obviously, dividing by
DaysPerYear is an option, however, i did want to check if there are
"built-in" functions that calculate this. Thanks.
 
J

Jon Skeet [C# MVP]

charliewest said:
There?s nothing wrong w/ the normal way if you know how ;-). Unfortunately, i
have been pampered by things like DateDiff. Obviously, dividing by
DaysPerYear is an option, however, i did want to check if there are
"built-in" functions that calculate this. Thanks.

DateTime birthday = ...;

TimeSpan age = DateTime.Today - birthday;

Then look at whatever you're interested in within the "age" value.
 
G

Guest

Hi Ansil - I like your answer but what if you want to report the age in
Years, Months, Days??
G
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top