Weird IE6 Date Comparison bug

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guilhermelemmi
    New Member
    • Sep 2008
    • 1

    Weird IE6 Date Comparison bug

    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:
    [CODE=javascript]function getPeriods(p_st artDate, p_duration, p_maxDate)
    {
    var periodsList = [];
    var nextStartDate, endDate, period, year, month, day;
    var startDt = p_startDate;
    do {
    year = startDt.getUTCF ullYear();
    month = startDt.getUTCM onth();
    day = startDt.getUTCD ate();

    nextStartDate = new Date();
    nextStartDate.s etFullYear(year , month + p_duration, day);

    endDate = new Date(nextStartD ate);
    endDate.setDate (endDate.getDat e()-1);

    if(endDate >= p_maxDate) {
    endDate = p_maxDate;
    }
    period = {"Start":startD t,"End":endDate };
    periodsList[periodsList.len gth] = period;
    startDt = nextStartDate;
    //alert(periodsLi st.length);
    }
    while(p_maxDate > endDate);

    return periodsList;
    }

    function printPeriods(p_ periodsList)
    {
    alert(">>>" + p_periodsList.l ength);
    //for(var i=0; i< p_periodsList.l ength ; i++)
    //{
    //alert("Start: " + p_periodsList[i].Start + "\nEnd: " + p_periodsList[i].End);
    //}
    }

    var st = new Date;
    st.setFullYear( 2008,0,1);
    var max = new Date;
    max.setFullYear (2009,0,1);
    max.setDate(max .getDate()-1);
    printPeriods(ge tPeriods(st, 3, max));[/CODE]


    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!
    Last edited by gits; Sep 10 '08, 09:25 PM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    Working...