Cool, I will take a look over it

I think I have just managed to irradicate the last of the issues with my code. So here it is as well. A bit of competition never hurts and hopefully that will make 2 working DateDiff functions!

VB.NET Code:
  1. Public Class Form1
  2.  
  3.     'Date1 is the lowest date, Date2 is the highest date
  4.     Private Sub DateCompare(ByVal Date1 As Date, ByVal Date2 As Date)
  5.  
  6.         Dim Days As Integer = Nothing
  7.         Dim Months As Integer = Nothing
  8.         Dim Years As Integer = Nothing
  9.  
  10.         'Convert Date1/Date2 months and years into months.
  11.         Dim Date1Months As Integer = Date1.Year * 12 + Date1.Month
  12.         Dim Date2Months As Integer = Date2.Year * 12 + Date2.Month
  13.         'Get the number of Months between the two dates. Subtract 1 (we will add this back later where necessary)
  14.         Months = Date2Months - Date1Months - 1
  15.  
  16.         'See how many days are within the Month Date1/Date2 reside.
  17.         Dim DaysInDate1Month As Integer = Date.DaysInMonth(Date1.Year, Date1.Month)
  18.         Dim DaysInDate2Month As Integer = Date.DaysInMonth(Date2.Year, Date2.Month)
  19.  
  20.  
  21.         If Date1.Day = DaysInDate1Month And Date2.Day = DaysInDate2Month Then
  22.             'If Both Date1 and Date2 occur on the LastDayOfTheMonth.
  23.  
  24.             'We count this as a month rather than x amount of days.
  25.             'Therefore add 1 to Months and set Days to zero.
  26.             'nb: We subtracted a month when we first grabbed the number of Months. Add it back here.
  27.             Months += 1
  28.             Days = 0
  29.         ElseIf Date1.Day = DaysInDate1Month And Date2.Day < DaysInDate2Month Then
  30.             'If Date1 occur's on the LastDayOfTheMonth but Date2 resides on a day prior to the end of it's month.
  31.  
  32.             'We know the number of Days between the two dates will be the number of Days in Date2.
  33.             Days = Date2.Day
  34.         Else
  35.             'If Neither Date1 or Date2 occur on the LastDayOfTheMonth then we can do a day count ourselves.
  36.             Select Case Date1.Day
  37.  
  38.                 Case Is < Date2.Day
  39.                     '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.
  40.  
  41.                     'The number of days will therefore be the difference between Date1/Date2's Day values.
  42.                     Days = Date2.Day - Date1.Day
  43.  
  44.                     'nb: We subtracted a month when we first grabbed the number of Months. Add it back here.
  45.                     Months += 1
  46.                 Case Is > Date2.Day
  47.                     '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.
  48.  
  49.                     'First we need to find out the month prior to Date2's month value.
  50.                     Dim MonthBeforeDate2 As Integer = Date2.Month
  51.                     'If Date2 occurs in January, then we will also need to use the previous year in our calculation.
  52.                     Dim Date2Year As Integer = Date2.Year
  53.                     If MonthBeforeDate2 = 1 Then
  54.                         MonthBeforeDate2 = 12
  55.                         Date2Year -= 1
  56.                     Else : MonthBeforeDate2 -= 1
  57.                     End If
  58.  
  59.                     '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.
  60.                     'As that would lead to a neagtive value.
  61.                     If Date1.Day > Date.DaysInMonth(Date2Year, MonthBeforeDate2) Then
  62.                         'In this case we only need the number of days in Date2.
  63.                         Days = Date2.Day
  64.                     Else
  65.                         '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.
  66.                         Dim DaysLeftInMonthBeforeDate2 As Long = Date.DaysInMonth(Date2Year, MonthBeforeDate2) - Date1.Day
  67.  
  68.                         'Finally we can add the number of Days in Date2.
  69.                         Days = DaysLeftInMonthBeforeDate2 + Date2.Day
  70.                     End If
  71.  
  72.                 Case Is = Date2.Day
  73.                     'If the Day value in Date1/Date2 matches.
  74.  
  75.                     'nb: We subtracted a month when we first grabbed the number of Months. Add it back here.
  76.                     Months += 1
  77.             End Select
  78.  
  79.         End If
  80.  
  81.         'The last step is to work out the number of Years. We can work this out from the 'Months'.
  82.         'nb: We left this until last because there may have been a few neccessary changes to the Months value along the way.
  83.         If Months >= 12 Then
  84.             Do Until Months < 12
  85.                 Years += 1
  86.                 Months -= 12
  87.             Loop
  88.         End If
  89.  
  90.         'Now just format the result:
  91.         MsgBox(String.Format("Years: {1}{0}Months: {2}{0}Days: {3}", {vbCrLf, Years, Months, Days}))
  92.  
  93.     End Sub
  94.  
  95. End Class
Now, about those weeks...