devxlogo

The age of a person

The age of a person

You can quickly evaluate the age of a person using the following function:

Function Age(BirthDate As Date, Optional CurrentDate As Variant) As Integer    If IsMissing(CurrentDate) Then CurrentDate = Now    Age = Year(CurrentDate) - Year(BirthDate)End Property

If you omit the second argument, the age is evaluate at the current time, but specifying it you can evaluate the age at any given date.

The above function, however, evaluates the age of a person in a “informal” way. To see what this means, run the following code:

Print Age(#6/1/1980#, #1/1/2000#)     ' returns 20

It is evident that the function actually returns the age of the person on his/her birthday in the current year, but the actual age could be less than this value. To evaluate the “exact” age, use the following variant:

Function Age(BirthDate As Date, Optional CurrentDate As Variant, _    Optional ExactAge As Boolean) As Integer    If IsMissing(CurrentDate) Then CurrentDate = Now    Age = Year(CurrentDate) - Year(BirthDate)        If ExactAge Then        ' subtract one if this year's birthday hasn't occurred yet        If DateSerial(Year(CurrentDate), Month(BirthDate), _            Day(BirthDate)) > CurrentDate Then            Age = Age - 1        End If    End IfEnd Property

devx-admin

Share the Post: