ASP.NET Calculating Age (VB)

I came across the issue today where I needed to calculate the age of a user of a system for the Equal Opportunities policy – there didn’t seem to be a good VB function out there – some of them were ridiculously long-winded, so I wrote one that is short and very processor efficient.

Function calculateAge(ByVal DateOfBirth As Date) As Integer

Dim TodaysDate As Date = Date.Today()

Dim Age As Integer = TodaysDate.Year - DateOfBirth.Year

If TodaysDate.Month < DateOfBirth.Month Or (TodaysDate.Month = DateOfBirth.Month And TodaysDate.Day < DateOfBirth.Day) Then
Age = Age - 1
End If

calculateAge = Age

Age = Nothing
TodaysDate = Nothing

End Function

Previous
Previous

PC Pro Podcast 57

Next
Next

PC Pro Podcast 56