# Calculating Ages

• ### Question

• User-1585994330 posted

When users register with my site they give me their DoB, how can I work out an age from it. I was thinkin about doing it from the year, but that isnt very accurate. Can anyone help? thanks si!

ps Id only want to work out how many years old they are. si!

Tuesday, July 3, 2007 9:40 AM

• User955742345 posted

This would work better, and you wouldn't have to calculate the years from days, which the timespan requires:

```DateTime _today = DateTime.Now.Date; // strip the time.
DateTime _DOB = DateTime.Parse(@"10/24/1980").Date; // strip the time.
int _Age = 0;

// get the difference in years.
_Age = _today.Year - _DOB.Year;

//our user was born in October.  If the current date is before October 24,
//  then the user hasn't had his birthday for this year.
//  2007 - 1980 = 27, 27-1 = 26 years old.
if ( _today.Month <= _DOB.Month && _today.Day <_DOB.Day )
{
//subtract 1
_Age -= 1;
}

//_Age now contains the correct age.```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Tuesday, July 3, 2007 10:34 AM

### All replies

• User955742345 posted

You started on the right track.  Subtract the DOB from Now.Date (or GETDATE()), and if the (DOB.Month >= Now.Month  And DOB.Day > Now.Day) then this year's birthday is in the future, so subtract 1.

Tuesday, July 3, 2007 9:54 AM
• User-1426124666 posted

Minus one date from the other and the result will be a TimeSpan. e.g.

`        Dim d As New Date        Dim t As New TimeSpan        d = System.DateTime.Today        t = d.Subtract("01/01/1950")`
You can then display their age by using the relevant properties from the TimeSpan object.
Tuesday, July 3, 2007 9:57 AM
• User-1585994330 posted

Sorry can you tell me what that is in C# please. Si!

Tuesday, July 3, 2007 10:03 AM
• User-1426124666 posted

There's no difference apart from how you declare the variables. e.g

```DateTime d = new DateTime();
TimeSpan t = new TimeSpan();
d = System.DateTime.Today;
t = d.Subtract("01/01/1950");```

Tuesday, July 3, 2007 10:10 AM
• User955742345 posted

This would work better, and you wouldn't have to calculate the years from days, which the timespan requires:

```DateTime _today = DateTime.Now.Date; // strip the time.
DateTime _DOB = DateTime.Parse(@"10/24/1980").Date; // strip the time.
int _Age = 0;

// get the difference in years.
_Age = _today.Year - _DOB.Year;

//our user was born in October.  If the current date is before October 24,
//  then the user hasn't had his birthday for this year.
//  2007 - 1980 = 27, 27-1 = 26 years old.
if ( _today.Month <= _DOB.Month && _today.Day <_DOB.Day )
{
//subtract 1
_Age -= 1;
}

//_Age now contains the correct age.```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Tuesday, July 3, 2007 10:34 AM
• User955742345 posted

Even though I used October above, the if statement applies to every possiblity.  This is as accurate as you can get in calculating age, as it takes into account whether the person has reached their birthday yet this year.

Tuesday, July 3, 2007 10:38 AM
• User-1041644243 posted

May be this could help for all of those who wanna calculate age in Days, Months and years.....

Thursday, January 10, 2008 12:04 PM