wDanHoward
Programmer
I am trying to calculate the amount months and days between to dates. The DateDiff function will give me the number of months or the number of days but not months and days. Is there a function in VBA or VB that does this?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Function DateDiff1(Dat1 As Date, Dat2 As Date) As Single
Dim iYr As Integer, iMo As Integer, iDa As Integer, D1 As Date, D2 As Date, iDaMx As Integer
If Dat1 > Dat2 Then
D2 = Dat1
D1 = Dat2
Else
D1 = Dat1
D2 = Dat2
End If
iYr = Year(D2) - Year(D1)
iMo = Month(D2) - Month(D1)
Select Case Month(D2)
Case 12
iDaMx = 31
Case Else
iDaMx = Day(DateSerial(Year(D2), Month(D2) + 1, 1) - 1)
End Select
iDa = Day(D2)
DateDiff1 = iYr * 12 + iMo + iDa / iDaMx
End Function