Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB4,VB5,VB6,VBS
Expertise: Beginner
Jun 17, 2000

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 If
End Property

Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date