+12  A: 

Do you want calculate the age in years for an employee? Then you can use this snippet (from http://stackoverflow.com/questions/9/how-do-i-calculate-someones-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.

alexn
Ya this is what i want to do...sorry for not phrasing it well..
Misnomer
Great! No problems.
alexn
A: 

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
Doesn't work - alexn has it
David M
This will only work for shorter spans, obviously, but if nobody's going to be more than 150 years old...
Kendrick
Yup, it doesn't work. Timespan needs a "TotalYears" property :-)
Kendrick
A: 

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
+3  A: 

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.

R. Bemrose
Nice answer!I wonder about the division with 365.25. Why is not just 365.0. I found that in other topics too and wonder if you can enlighten me.
ppolyzos
.25 is due to leap years
Mikael Svenson
I also fixed it to use Days instead of TotalDays. TotalDays is a double, not an int. Since we don't care about partial days anyway...
R. Bemrose
A: 
(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
A: 
    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
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