calculation of age from date of birth

J

Julian

Hi

In VB .Net how do you calculate someones age from their date of birth. I
have the dob in dd/mm/yyyy format and was using date difference, but this
just gives the years with out taking into account whether the person has had
their birth day that year. Ideally I wanted to caclulate it in both years
and in years plus months.

Many thanks

Julian
 
R

Rad [Visual C# MVP]

Hi

In VB .Net how do you calculate someones age from their date of birth. I
have the dob in dd/mm/yyyy format and was using date difference, but this
just gives the years with out taking into account whether the person has had
their birth day that year. Ideally I wanted to caclulate it in both years
and in years plus months.

Many thanks

Julian

The Timespan class is what you're looking for
 
Joined
Apr 16, 2008
Messages
1
Reaction score
0
Code:
		public string Age
		{
			get 
			{
				if (!dob.HasValue)
					return string.Empty;

				int tmpResult = DateTime.Now.Year - dob.Value.Year;

				if ((DateTime.Now.Month >= dob.Value.Month) 
					&& (DateTime.Now.Day >= dob.Value.Day))
				{
					tmpResult += 1;
				}

				return tmpResult.ToString();
			}
		}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top