Home  |   French  |   About  |   Search  | mvps.org  

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

In Memoriam

Terms of Use


VB Petition

Date/Time: Calculate Age of a person

Author(s)
Dev Ashish,
Michel Walsh &
Tim Walters

(Q) How do I calculate the age of a person given his/her birthdate?

(A) There are several methods to do this. I'll list two methods that have been posted to the newsgroups in the recent past:

Assuming that the birthdate field is called [BDate] and is of type date, you can use the following calculation

    Age=DateDiff("yyyy", [Bdate], Now())+ _
            Int( Format(now(), "mmdd") < Format( [Bdate], "mmdd") )

Alternate: You can use this function to calculate Age.

Function Age(Bdate, DateToday) As Integer
' Returns the Age in years between 2 dates
' Doesn't handle negative date ranges i.e. Bdate > DateToday


    If Month(DateToday) < Month(Bdate) Or (Month(DateToday) = _
                Month(Bdate) And Day(DateToday) < Day(Bdate)) Then
            Age = Year(DateToday) - Year(Bdate) - 1
    Else
            Age = Year(DateToday) - Year(Bdate)
    End If
End Function

    Here's another detailed Age Checker.

'--- CODE START ---
Public Sub CalcAge(vDate1 As Date, vdate2 As Date, ByRef vYears As Integer,
ByRef vMonths As Integer, ByRef vDays As Integer)
    ' Comments  : calculates the age in Years, Months and Days
    ' Parameters:
    '    vDate1 - D.O.B.
    '    vDate2 - Date to calculate age based on
    '    vYears - will hold the Years difference
    '    vMonths - will hold the Months difference
    '    vDays - will hold the Days difference
    vMonths = DateDiff("m", vDate1, vdate2)
    vDays = DateDiff("d", DateAdd("m", vMonths, vDate1), vdate2)
    If vDays < 0 Then
        ' wierd way that DateDiff works, fix it here
        vMonths = vMonths - 1
        vDays = DateDiff("d", DateAdd("m", vMonths, vDate1), vdate2)
    End If
    vYears = vMonths \ 12 ' integer division
    vMonths = vMonths Mod 12 ' only want leftover less than one year
End Sub
'--- CODE END ---

© 1998-2010, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer