VB Code:
Public Function IntervalBTDates(ByVal StartDate As Date, ByVal EndDate As Date) As String
'
' Returns the interval between 2 dates in years, months & days
'
Dim dd As Long
Dim mm As Long
Dim yy As Long
Dim ret As String
Dim tmpDate As Date
'
' Do some basic validation before going any further
'
If StartDate > EndDate Then
MsgBox "Start date must be less than the end date.", vbExclamation, "Date Error"
Exit Function
ElseIf StartDate = EndDate Then
IntervalBTDates = "0 years, 0 months, 0 days"
Exit Function
End If
dd = DateDiff("d", StartDate, EndDate)
mm = DateDiff("m", StartDate, EndDate)
If mm > 0 Then
If Day(StartDate) > Day(EndDate) Then
mm = mm - 1
End If
End If
yy = mm \ 12
mm = mm Mod 12
tmpDate = DateAdd("yyyy", -yy, EndDate)
tmpDate = DateAdd("m", -mm, tmpDate)
dd = Abs(DateDiff("d", StartDate, tmpDate))
If yy = 1 Then
ret = "1 year, "
Else
ret = CStr(yy) & " years, "
End If
If mm = 1 Then
ret = ret & "1 month, "
Else
ret = ret & CStr(mm) & " months, "
End If
If dd = 1 Then
ret = ret & "1 day"
Else
ret = ret & CStr(dd) & " days"
End If
IntervalBTDates = ret
End Function