Public Class Form1
'Date1 is the lowest date, Date2 is the highest date
Private Sub DateCompare(ByVal Date1 As Date, ByVal Date2 As Date)
Dim Days As Integer = Nothing
Dim Months As Integer = Nothing
Dim Years As Integer = Nothing
'Convert Date1/Date2 months and years into months.
Dim Date1Months As Integer = Date1.Year * 12 + Date1.Month
Dim Date2Months As Integer = Date2.Year * 12 + Date2.Month
'Get the number of Months between the two dates. Subtract 1 (we will add this back later where necessary)
Months = Date2Months - Date1Months - 1
'See how many days are within the Month Date1/Date2 reside.
Dim DaysInDate1Month As Integer = Date.DaysInMonth(Date1.Year, Date1.Month)
Dim DaysInDate2Month As Integer = Date.DaysInMonth(Date2.Year, Date2.Month)
If Date1.Day = DaysInDate1Month And Date2.Day = DaysInDate2Month Then
'If Both Date1 and Date2 occur on the LastDayOfTheMonth.
'We count this as a month rather than x amount of days.
'Therefore add 1 to Months and set Days to zero.
'nb: We subtracted a month when we first grabbed the number of Months. Add it back here.
Months += 1
Days = 0
ElseIf Date1.Day = DaysInDate1Month And Date2.Day < DaysInDate2Month Then
'If Date1 occur's on the LastDayOfTheMonth but Date2 resides on a day prior to the end of it's month.
'We know the number of Days between the two dates will be the number of Days in Date2.
Days = Date2.Day
Else
'If Neither Date1 or Date2 occur on the LastDayOfTheMonth then we can do a day count ourselves.
Select Case Date1.Day
Case Is < Date2.Day
'If Date2's Day is higher then Date1's Day then the 'Months' value will represent the number of months upto the Month that Date2 resides.
'The number of days will therefore be the difference between Date1/Date2's Day values.
Days = Date2.Day - Date1.Day
'nb: We subtracted a month when we first grabbed the number of Months. Add it back here.
Months += 1
Case Is > Date2.Day
'If Date2's Day is lower then Date1's Day then the 'Months' value will represent the number of months upto the Month prior to that which Date2 resides.
'First we need to find out the month prior to Date2's month value.
Dim MonthBeforeDate2 As Integer = Date2.Month
'If Date2 occurs in January, then we will also need to use the previous year in our calculation.
Dim Date2Year As Integer = Date2.Year
If MonthBeforeDate2 = 1 Then
MonthBeforeDate2 = 12
Date2Year -= 1
Else : MonthBeforeDate2 -= 1
End If
'If there are more days in Date1 than occur in the month prior to Date2 then we want to avoid calculating the days that remain in that month.
'As that would lead to a neagtive value.
If Date1.Day > Date.DaysInMonth(Date2Year, MonthBeforeDate2) Then
'In this case we only need the number of days in Date2.
Days = Date2.Day
Else
'Otherwise next we will see how many days occur between the value of Date1's Day and the end of the month prior to Date2.
Dim DaysLeftInMonthBeforeDate2 As Long = Date.DaysInMonth(Date2Year, MonthBeforeDate2) - Date1.Day
'Finally we can add the number of Days in Date2.
Days = DaysLeftInMonthBeforeDate2 + Date2.Day
End If
Case Is = Date2.Day
'If the Day value in Date1/Date2 matches.
'nb: We subtracted a month when we first grabbed the number of Months. Add it back here.
Months += 1
End Select
End If
'The last step is to work out the number of Years. We can work this out from the 'Months'.
'nb: We left this until last because there may have been a few neccessary changes to the Months value along the way.
If Months >= 12 Then
Do Until Months < 12
Years += 1
Months -= 12
Loop
End If
'Now just format the result:
MsgBox(String.Format("Years: {1}{0}Months: {2}{0}Days: {3}", {vbCrLf, Years, Months, Days}))
End Sub
End Class