calculation of age from date of birth

  • Thread starter Thread starter Julian
  • Start date Start date
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
 
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
 
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();
			}
		}
 
Back
Top