Expression to show years and month difference between 2 dates (1 Viewer)

jimtimber

Registered User.
Local time
Today, 14:58
Joined
Apr 25, 2014
Messages
118
Hi,

I am wanting to get an expression that will return the difference in years and months between 2 dates. Specifically, I want to know peoples ages in years and months based on a person's D.O.B and todays date. I have managed to do it in years:

Expr1: DateDiff("yyyy",[TBL_EmployeeDetails]![D_O_B],Date())

and in months but I would like to know how to return the difference in years and months.

Is there an easier way of doing this please?

Thanks,

Jim
 

pr2-eugin

Super Moderator
Local time
Today, 14:58
Joined
Nov 30, 2011
Messages
8,494
This code might give something like that?
Code:
Public Function exDateDiff(Date1 As Date, Date2 As Date) As String
[COLOR=Green]'***********************************
'Code Courtesy of
'  Paul Eugin
'   ? exDateDiff(#5/3/1989#, Date)
'     25 Years 0 Month and 3 days.
'***********************************[/COLOR]
    Dim totDays As Long, yVar As Long, mVar As Long, dVar As Long
    
    totDays = DateDiff("d", Date1, Date2)
    
    If Year(Date1) <> Year(Date2) Then
        While totDays > 365
            totDays = totDays - Val(Format(DateSerial(Year(Date1) + yVar, 12, 31), "Y"))
            yVar = yVar + 1
        Wend
    Else
        yVar = 0
    End If
    
    While totDays > 30
        totDays = totDays - DatePart("d", DateSerial(Year(Date1) + yVar, Month(Date1) + mVar + 1, 0))
        mVar = mVar + 1
    Wend
    
    dVar = totDays
    
    exDateDiff = yVar & IIf(yVar <= 1, " Year ", " Years ") & _
                 mVar & IIf(mVar <= 1, " Month and ", " Months and ") & _
                 dVar & IIf(dVar <= 1, " Day.", " Days.")
End Function
 

Users who are viewing this thread

Top Bottom