4

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

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • If you are interested in the "opposite" direction (converting fractional years to days/dates) see: https://stackoverflow.com/questions/14859332/how-to-convert-fractional-years-say-1-25-years-to-number-of-days/45389987#45389987. – Ohad Schneider Jul 29 '17 at 13:56

6 Answers6

11

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
  • 687,336
  • 108
  • 737
  • 1,005
  • 3
    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 Mar 23 '09 at 15:10
  • Should have been: If you add 1 year it gives you 2009-02-28 instead of 2009-03-01. – Will Rickards Mar 23 '09 at 15:53
  • I guess it depends on whether you view 2008-02-29 as one day after 2008-02-28 or one day before 2008-03-01... I agree that neither one is obviously more correct. – Ohad Schneider Jul 29 '17 at 12:49
6

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#?

Community
  • 1
  • 1
splattne
  • 102,760
  • 52
  • 202
  • 249
1

An approximite would be:

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

        TimeSpan age = DateTime.Now.Subtract(bd);
        Console.WriteLine(age.TotalDays / 365.25);
JoshBerke
  • 66,142
  • 25
  • 126
  • 164
1

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
  • 2,776
  • 2
  • 19
  • 25
0

You could do this:

Console.WriteLine(DateTime.Now.Date.Subtract(new DateTime(1980, 8, 1)).TotalDays / 365.25);
eKek0
  • 23,005
  • 25
  • 91
  • 119
0

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
  • 11,334
  • 2
  • 25
  • 33