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

See also  How to Block Websites During Certain Hours on a Mac Computer
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist