56

Possible Duplicate:
How do I calculate someone’s age in C#?

I want to calculate basically the age of employees - So we have DOB for each employee, So on the C# Side I want to do something like this -

int age=Convert.Int32(DateTime.Now-DOB);

I can use days and manipulate then get the age...but I wanted to know if there something I can use directly to get the number of years.

Community
  • 1
  • 1
Vishal
  • 12,133
  • 17
  • 82
  • 128

6 Answers6

91

Do you want calculate the age in years for an employee? Then you can use this snippet (from Calculate age in C#):

DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday > now.AddYears(-age)) age--;

If not, then please specify. I'm having a hard time understanding what you want.

Community
  • 1
  • 1
alexn
  • 57,867
  • 14
  • 111
  • 145
  • Ya this is what i want to do...sorry for not phrasing it well.. – Vishal Jun 30 '10 at 20:10
  • Hi, why do you decrement the age in the 3rd line? – WTFZane Mar 14 '17 at 02:57
  • @WTFZane Because if the date that year is greater than the date in the current year, then they aren't that age yet. e.g. DOB = 30/5/2000. Today = 30/4/2010. age = 10. Then adjust to 9 as they are only 9 years old. – Ian Jul 12 '17 at 09:30
  • Note this fails, by 1 day, for anyone born on 29th Feb as their birthday in a non-leap year is 1 March (i.e. "the day after 28 Feb") in certain countries (e.g. UK). That is because `AddYears()` returns 28 Feb for any non-leap year (if the date of birth input is a valid 29 Feb date). An ideal solution would include the country as a parameter :) – iCollect.it Ltd Sep 25 '20 at 15:19
  • Do this to take into consider leap years also DateTime.Today.Year - DateOfBirth.Year +(DateTime.Today.DayOfYear < DateOfBirth.DayOfYear?-1:0) – Ravan Dec 16 '21 at 06:28
20

Subtracting two DateTime gives you a TimeSpan back. Unfortunately, the largest unit it gives you back is Days.

While not exact, you can estimate it, like this:

int days = (DateTime.Today - DOB).Days;

//assume 365.25 days per year
decimal years = days / 365.25m;

Edit: Whoops, TotalDays is a double, Days is an int.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
13

On this site they have:

   public static int CalculateAge(DateTime BirthDate)
   {
        int YearsPassed = DateTime.Now.Year - BirthDate.Year;
        // Are we before the birth date this year? If so subtract one year from the mix
        if (DateTime.Now.Month < BirthDate.Month || (DateTime.Now.Month == BirthDate.Month && DateTime.Now.Day < BirthDate.Day))
        {
            YearsPassed--;
        }
        return YearsPassed;
  }
Kyra
  • 5,129
  • 5
  • 35
  • 55
4
    private static Int32 CalculateAge(DateTime DOB)
    {
        DateTime temp = DOB;
        Int32 age = 0;
        while ((temp = temp.AddYears(1)) < DateTime.Now)
            age++;
        return age;
    }
matt-dot-net
  • 4,204
  • 21
  • 24
  • dividing by 365 doesn't handle leap years and actually dividing by 365.25 doesn't actually handle leap years accurately either. – matt-dot-net Jul 01 '10 at 04:56
  • This is actually closer, to a working solution, but it is out by a day. Should be `<= DateTime.Now` in your specific example. Born 28/1/1980 -> 28/1/1981 = 0 in your example – iCollect.it Ltd Sep 25 '20 at 15:13
0

Math.Round(DateTime.Now.Subtract(DOB).TotalDays/365.0)

As pointed out, this won't work. You'd have to do this:

(Int32)Math.Round((span.TotalDays - (span.TotalDays % 365.0)) / 365.0);

and at that point the other solution is less complex and continues to be accurate over larger spans.

Edit 2, how about:

Math.Floor(DateTime.Now.Subtract(DOB).TotalDays/365.0)

Christ I suck at basic math these days...

Kendrick
  • 3,747
  • 1
  • 23
  • 41
  • This will only work for shorter spans, obviously, but if nobody's going to be more than 150 years old... – Kendrick Jun 30 '10 at 20:10
  • Yup, it doesn't work. Timespan needs a "TotalYears" property :-) – Kendrick Jun 30 '10 at 20:16
  • Sorry to flog a dead horse here but this also doesn't take into account leap years, so it'll return incorrect results in a time span of as little as 1 year. – Mark Feldman Mar 12 '15 at 21:25
-4
(DateTime.Now - DOB).TotalDays/365

Subtracting a DateTime struct from another DateTime struct will give you a TimeSpan struct which has the property TotalDays... then just divide by 365

astorcas
  • 269
  • 4
  • 13