0

Please can anyone shed any light on this for me, and possibly suggest a solution.

I am creating a custom calendar which will be used to schedule events.

In the constructor I pass in 2 dates (startDate and endDate)

The form has a FlowLayoutPanel which is then populated with UserControls for the Months.

The issue I am having is that when I do a DateDiff(DateInterval.Month, startDate, endDate) with the following Dates: startDate = 22/11/2012, endDate = 28/02/2013 the result is 3.

BUT, actually, on a calendar, I would need to display 4 Months - Nov, Dec, Jan & Feb.

That said though, the logic works correctly for startDate = 12/11/2012, endDate = 01/03/2012

Richard Gale
  • 1,816
  • 5
  • 28
  • 45
  • Well, the interval is just over 3 months, so it is soemthing like 3 month and 6 days (or so). You are getting the correct value for full months, but seem to want to also have the extra days counted as another month. – Oded Nov 27 '12 at 11:01
  • But even if I fix the dates, i.e. Set startDate = 01/11/2012 and set endDate = 31/02/2013 I still don't get the 4 months. – Richard Gale Nov 27 '12 at 11:04
  • sorry, I meant endDate = 28/02/2013 – Richard Gale Nov 27 '12 at 11:45

2 Answers2

2

From the MSDN docs on DateDiff

If Interval is set to DateInterval.Year, the return value is calculated purely from the year parts of Date1 and Date2. Similarly, the return value for DateInterval.Month is calculated purely from the year and month parts of the arguments, and for DateInterval.Quarter from the quarters containing the two dates.

For example, when comparing December 31 to January 1 of the following year, DateDiff returns 1 for DateInterval.Year, DateInterval.Quarter, or DateInterval.Month, even though at most only one day has elapsed.

This means that the calculation doesn't accounts for the remainder 'days',

Probably you should build your custom calendar not using DateDiff to found the months involved.
Instead your should work using a pseudocode like this:

 Dim curMonth = startDate.Month
 Dim curYear = startDate.Year
 while curMonth <= endDate.Month andalso curYear <= endDate.Year
     AddCalendar(curMonth, curYear)
     curMonth = curMonth + 1
     if curMonth > 12 then 
         curMonth = 1
         curYear = curYear + 1
     end if
 end while
Steve
  • 213,761
  • 22
  • 232
  • 286
0

If I understand correctly you'll want to show 4 months even if the actual difference between the dates is less than 3 months. One solution is something like this:

Dim startDate = #11/22/2012# 'Nov 22nd
Dim endDate = #2/2/2013# 'Feb 2nd

Dim startMonth = new DateTime (startDate.Year, startDate.Month, 1)
Dim endMonth = new DateTime (endDate.Year, endDate.Month, 1).AddMonths (1)
Dim diff = DateDiff (DateInterval.Month, startMonth, endMonth)
Rolf Bjarne Kvinge
  • 19,253
  • 2
  • 42
  • 86
  • I want the calendar to show ALL months within the date range, that is correct. BUT, I do not want to show an extra, unrequired month, and I am worried your solution may do this in instances where dateDiff works fine now. – Richard Gale Nov 27 '12 at 11:46
  • My solution includes the starting month, the ending month and all the months in between (and nothing else). – Rolf Bjarne Kvinge Nov 27 '12 at 13:46