> Given two dates I would like to calculate the difference between them
in years months and days. Datediff does not seem to do what I need.
S_DATE = 10-AUG-2002
E_DATE = 11-AUG-2002
Result = 1 year, 0 month, 1 day
That looks like only 1 day to me.<g>
Has anyone got a routine for this or do I need to code one up
taking into account leap years , days in months spanned etc etc.
This is a modification of some code I've posted in the past. I think it does
what you want. Note that is properly singularizes and/or pluralizes the text
parts.
Rick - MVP
Function YMD(StartDate As Date, EndDate As Date) As String
Dim TempDate As Date
Dim NumOfYears As Long
Dim NumOfMonths As Long
Dim NumOfWeeks As Long
Dim NumOfDays As Long
Dim NumOfHMS As Double
Dim TSerial1 As Double
Dim TSerial2 As Double
NumOfYears = DateDiff("yyyy" , StartDate, EndDate)
TSerial1 = TimeSerial(Hour (StartDate), _
Minute(StartDat e), Second(StartDat e))
TSerial2 = TimeSerial(Hour (EndDate), _
Minute(EndDate) , Second(EndDate) )
NumOfHMS = 24 * (TSerial2 - TSerial1)
If NumOfHMS < 0 Then
NumOfHMS = NumOfHMS + 24
EndDate = DateAdd("d", -1, EndDate)
End If
StartDate = DateSerial(Year (EndDate), _
Month(StartDate ), Day(StartDate))
If StartDate > EndDate Then
StartDate = DateAdd("yyyy", -1, StartDate)
NumOfYears = NumOfYears - 1
End If
NumOfMonths = DateDiff("m", StartDate, EndDate)
StartDate = DateSerial(Year (EndDate), _
Month(EndDate), Day(StartDate))
If StartDate > EndDate Then
StartDate = DateAdd("m", -1, StartDate)
NumOfMonths = NumOfMonths - 1
End If
NumOfDays = Abs(DateDiff("d ", StartDate, EndDate))
YMD = CStr(NumOfYears ) & " year" & _
IIf(NumOfYears = 1, "", "s")
YMD = YMD & ", "
YMD = YMD & CStr(NumOfMonth s) & " month" & _
IIf(NumOfMonths = 1, "", "s")
YMD = YMD & ", "
YMD = YMD & CStr(NumOfDays) & " day" & _
IIf(NumOfDays = 1, "", "s")
End Function