Uploaded January 2020 | Updated September 2026, 1 week ago
VB.Net code to determine a person's age from birthdate using the date diff function.
Visual Studio Community 2017
.NET Framework 4.6.1
Windows 10
#vbdotnet
Code below.
'Angled brackets not allowed so replace "GREATER_THAN'
'Calculate age from birthdate
Private Function Age(ByVal birthdate As DateTime) As String
Dim ageDays As Integer
Dim ageMonths As Integer
Dim ageYears As Integer
ageDays = DateDiff("d", birthdate, Now)
ageMonths = DateDiff("m", birthdate, Now)
'Reduce month by 1 if birthdate day not yet reached
If birthdate.Day GREATER_THAN Now.Day Then
ageMonths = ageMonths - 1
End If
'Calculate years (Do not round value up)
ageYears = Math.Truncate(ageMonths / 12) 'can use \ operater as well
If ageYears GREATER_THAN 0 Then
Return ageYears.ToString + Plural(ageYears, " Year")
ElseIf ageMonths GREATER_THAN 0 Then
Return ageMonths.ToString + Plural(ageMonths, " Month")
Else
Return ageDays.ToString + Plural(ageDays, " Day")
End If
End Function
'Return plural form of text if value not = 1
Private Function Plural(value As Integer, text As String)
If Math.Abs(value) = 1 Then
Return text
Else
Return text + "s"
End If
End Function
VB.Net code to determine a person's age from birthdate using the date diff function.
Visual Studio Community 2017
.NET Framework 4.6.1
Windows 10
#vbdotnet
Code below.
'Angled brackets not allowed so replace "GREATER_THAN'
'Calculate age from birthdate
Private Function Age(ByVal birthdate As DateTime) As String
Dim ageDays As Integer
Dim ageMonths As Integer
Dim ageYears As Integer
ageDays = DateDiff("d", birthdate, Now)
ageMonths = DateDiff("m", birthdate, Now)
'Reduce month by 1 if birthdate day not yet reached
If birthdate.Day GREATER_THAN Now.Day Then
ageMonths = ageMonths - 1
End If
'Calculate years (Do not round value up)
ageYears = Math.Truncate(ageMonths / 12) 'can use \ operater as well
If ageYears GREATER_THAN 0 Then
Return ageYears.ToString + Plural(ageYears, " Year")
ElseIf ageMonths GREATER_THAN 0 Then
Return ageMonths.ToString + Plural(ageMonths, " Month")
Else
Return ageDays.ToString + Plural(ageDays, " Day")
End If
End Function
'Return plural form of text if value not = 1
Private Function Plural(value As Integer, text As String)
If Math.Abs(value) = 1 Then
Return text
Else
Return text + "s"
End If
End Function










