It’s quite common to need to calculate a person’s age from the date of birth they have entered. Here’s the quick and easy way of doing it:

Dim strDOB As String

strDOB = txtDateOfBirth.TextDim intAge As Integer

intAge = Math.Floor(DateDiff(DateInterval.Month, DateValue(strDOB), Now()) / 12)

lblAge.Text = intAge

In this example the date of birth data is captured in a TextBox control called txtDateOfBirth. The DateDiff function calculates the difference between two dates. The arguments are the interval to focus on (here months), the first date (here the date of birth) and the second date (here the current date). The date of birth is expressed as DateValue(strDOB) to convert the string to a date format. This is then divided by 12 to give the age in years as a decimal. Finally, the introductory Math.Floor is used to round the number down to the nearest whole number.

## About Chris Smith

Web developer specialising in frontend UI design

@chris22smith
help pls i need to calculate age using datetimespicker , when i click button the result in the text box

This is my code for calculate age

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim birthday As Date

Dim today As Date = Date.Now

Dim span As System.TimeSpan

birthday = Me.DateTimePicker1.Value

span = today.Subtract(birthday)

End Sub

If (Month(Date.Today) * 100) + Date.Today.Day >= (Month(dtpBOfD.Value) * 100) + dtpBOfD.Value.Day Then lblAge.Text = DateDiff(DateInterval.Year, dtpBOfD.Value, Date.Today) Else lblAge.Text = DateDiff(DateInterval.Year, dtpBOfD.Value, Date.Today) – 1 End If

if based on months the calculation for someone born on tomorrow’s date a year ago will be incorrectly 1

why not make it based on days and not months

intAge = Math.Floor(DateDiff(DateInterval.Day, DateValue(strDOB), Now()) / 365.25)

and if entering anyone’s ages, including todders the only divide by the .25 on the 365 if under four

Hey Chris, Thanks for the post. It got me started looking for a simple, elegant way to determine age in .NET. Unfortunatley, DateDiff, when preforming comparisons with the DateInterval.Month, will only calculate meaning that comparisons of dates from the same month but not yet overlapping, will come up in the wrong year,as @Rob pointed out. Instead try using the following function:

Public Function GetCurrentAge(ByVal dob As Date) As Integer

page = Today.Year - dob.Year

If (dob > Today.AddYears(-age)) Then age -= 1

Return age

End Function

You can check out my example here

Hey Chris, Thanks for the post. It got me started looking for a simple, elegant way to determine age in .NET. Unfortunatley, DateDiff, when preforming comparisons with the DateInterval.Month, will only calculate meaning that comparisons of dates from the same month but not yet overlapping, will come up in the wrong year,as @Rob pointed out. Instead try using the following function:

Public Function GetCurrentAge(ByVal dob As Date) As Integer

   Dim age As Integer

   age = Today.Year - dob.Year

   If (dob > Today.AddYears(-age)) Then age -= 1

   Return age

End Function

its working fine

Why not do this:

Private Function CalculateAge() As Integer

Dim dtNow As DateTime = DateTime.Now

Dim dtBirth As DateTime = New DateTime(1981, 1, 24)

Dim dtBirthThisYear As DateTime = New DateTime(dtNow.Year, dtBirth.Month, dtBirth.Day)

Dim yearDifference As Integer = dtNow.Year – dtBirth.Year

If yearDifference = 0 Then Return 0

Dim dayDifference As Integer = (dtNow – dtBirthThisYear).TotalDays

If dayDifference >= 0 Then

Return yearDifference

Else

Return yearDifference – 1

End If

End Function

Private Sub DateTimePicker1_ValueChanged() Handles DateTimePicker1.ValueChanged

Dim Birth As Date = DateTimePicker1.Value

Dim year, month, day As Integer

While year <= (Now.Year - Birth.Year)

While month <= (Now.Month - Birth.Month)

While day <= (Now.Day - Birth.Day)

day += 1

End While

month += 1

End While

year += 1

End While

year -= 1 : month -= 1 : day -= 1

MsgBox(year & " year " & month & " month " & day & " day")

End Sub

Visit This

Dim result As Integer

Dim rema As Integer

Dim startdate As Date = CDate(DOB.Text)

Dim totalmonths As Integer = DateDiff(DateInterval.Month, startdate, Now)

result = Math.DivRem(totalmonths, 12, rema)

If startdate.Month = Now.Month Then

Dim eldia As Integer = startdate.Day

If eldia > Now.Day Then

result -= 1

End If

End If

lblresult.Text = result

The dates I’ve given are in DD/MM/YYYY format. Someone born on 2nd Sep 2009 would have been 1 on 2nd Sep 2010 and would not yet be 2 (as of today, 11th Dec 2010) so this seems right to me. Someone born on 2nd Jan 2010 would not yet be 1 but nearly 1 so, it’s not quite right in terms of age but the person would be closer to being 1 than 0.

It’s an approximate based on the count of months and rounded to the nearest whole number of years. So, if it’s within one month, i.e. 11 months, it’s rounded up. Not perfect but ok as a “quick and dirty” age calcultation. Yo’d have to go down to counts of days to be accurate.

What happens if you run the algorithm against 02/09/2009 and 02/01/2010? I am currently getting 1 years of age, which they are not.