tags:

views:

972

answers:

6

Hi,

How can I get the age of someone given the date of birth in a C# datetime.

I want a precise age like 40.69 years old

+4  A: 

This would be an approximative calculation:

 TimeSpan span = DateTime.Today.Subtract(birthDate);
 Console.WriteLine( "Age: " + (span.TotalDays / 365.25).toString() );

BTW: see also this question on Stack Overflow: How do I calculate someone’s age in C#?

splattne
why are you multiplying it with 365.25?
TStamper
He meant "/"
The .25 is for the leap years.
sorry "/" is right. I edited my answer.
splattne
You could use "span.TotalDays" to approximate even better
MartinStettner
thanks for the suggestions!
splattne
how come everyone is suggesting .Subtract()? I like "DateTime.Today - birthDate" better :)
Lucas
+1  A: 

An approximite would be:

        DateTime bd = new DateTime(1999, 1, 2);

        TimeSpan age = DateTime.Now.Subtract(bd);
        Console.WriteLine(age.TotalDays / 365.25);
JoshBerke
A: 

You could do this:

Console.WriteLine(DateTime.Now.Date.Subtract(new DateTime(1980, 8, 1)).TotalDays / 365.25);
eKek0
A: 

how much error is allowed in the fractional portion? a precise age would be

31 years, 10 days, 3 hours, etc. depending on the precision wanted.

dbasnett
+1  A: 

The 40 years part is easy enough. But to get a truly accurate decimal point, I'm not sure how you translate the rest of the age into a decimal number. You see age is expressed in Years, Months, Days, Hours, Seconds. And the calculation isn't that easy. You have to deal with anniversary dates. Like if someone was born on January 31st, when are they 1 month old? The answer is March 1st. But in some years that is 28 days later and some years 29 days later. Here is a javascript implementation I did that tries to deal with this.

But I suppose the decimal could express the number of days since the most recent birthday anniversay divided by the number of days till the next birthday anniversary. And if you wanted to get more precise you could do it in seconds using the same principle.

But I think it is a poor representation of an age. We just don't usually represent an age like that.

And make sure your datetimes are in the same timezone for your comparisons.

Will Rickards
+4  A: 

This will calculate the exact age. The fractional part of the age is calculated relative to the number of days between the last and the next birthday, so it will handle leap years correctly.

The fractional part is linear across the year (and doesn't take into account the different lengths of the months), which seems to make most sense if you want to express a fractional age.

// birth date
DateTime birthDate = new DateTime(1968, 07, 14);

// get current date (don't call DateTime.Today repeatedly, as it changes)
DateTime today = DateTime.Today;
// get the last birthday
int years = today.Year - birthDate.Year;
DateTime last = birthDate.AddYears(years);
if (last > today) {
 last = last.AddYears(-1);
 years--;
}
// get the next birthday
DateTime next = last.AddYears(1);
// calculate the number of days between them
double yearDays = (next - last).Days;
// calcluate the number of days since last birthday
double days = (today - last).Days;
// calculate exaxt age
double exactAge = (double)years + (days / yearDays);
Guffa
On the get the next birthday line, AddYears doesn't handle leap years correctly regarding age. Say your birthday is february 29th, 2008. If you add 1 year it gives you 2008-02-28 instead of 2008-03-01.
Will Rickards
Should have been: If you add 1 year it gives you 2009-02-28 instead of 2009-03-01.
Will Rickards
Well, why would 2009-03-01 be more correct than 2009-02-28?
Guffa