By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Weird IE6 Date Comparison bug

P: 1
I'm creating a function that will take a start date, a period duration (in months)and a maximum date, and will calculate the periods start and end dates until reaching the maximum date.
For instance, I would give 01/01/2008, period equals 3 months and maximum date equals 01/01/2009, and the method should return periods like "01/01/2008-03/31/2008","04/01/2008-06/30/2008","07/01/2008-09/30/2008","10/01/2008-12/31/2008".

The code looks like:
Expand|Select|Wrap|Line Numbers
  1. function getPeriods(p_startDate, p_duration, p_maxDate)
  2. {
  3.   var periodsList = [];
  4.   var nextStartDate, endDate, period, year, month, day;
  5.   var startDt = p_startDate; 
  6.   do {
  7.     year = startDt.getUTCFullYear();
  8.     month = startDt.getUTCMonth();
  9.     day = startDt.getUTCDate();
  10.  
  11.     nextStartDate = new Date();
  12.     nextStartDate.setFullYear(year, month + p_duration, day);
  13.  
  14.     endDate = new Date(nextStartDate);
  15.     endDate.setDate(endDate.getDate()-1);
  16.  
  17.     if(endDate >= p_maxDate) {
  18.       endDate = p_maxDate;
  19.     }
  20.     period = {"Start":startDt,"End":endDate};
  21.     periodsList[periodsList.length] = period;
  22.     startDt = nextStartDate;
  23.     //alert(periodsList.length);
  24.   }
  25.   while(p_maxDate > endDate); 
  26.  
  27.   return periodsList;
  28. }
  29.  
  30. function printPeriods(p_periodsList)
  31. {
  32.   alert(">>>" + p_periodsList.length);
  33.   //for(var i=0; i< p_periodsList.length ; i++)
  34.   //{
  35.     //alert("Start: " + p_periodsList[i].Start + "\nEnd: " + p_periodsList[i].End);
  36.   //}
  37. }
  38.  
  39. var st = new Date;
  40. st.setFullYear(2008,0,1);
  41. var max = new Date;
  42. max.setFullYear(2009,0,1);
  43. max.setDate(max.getDate()-1);
  44. printPeriods(getPeriods(st, 3, max));

However, when I run the code, the alert window will display ">>> 5" in IE6, while it displays ">>> 4" in Firefox, which is the expected length.

To make things even stranger, if I uncomment the "alert" in the last line of the "do" block, IE will run the correct number of loops, displaying ">>> 4" in the end.

Any ideas or workarounds?

Thanks in advance!
Sep 10 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
Weird indeed. This also occurs in IE7. Since it works with the alert, there must be some timing bug. It may work if you use a small timeout.
Sep 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.