views:

80

answers:

3

I have been using a tidy little routine that I found here to calculate the difference in days between two dates in AS3. I am getting some strange results and I am wondering if any of you inter-codal-mega-lords can shed some light?

Why is Q1 of 2010 coming up one day short, when in all other cases the routine is performing fine?

Many thanks in advance to anyone who can help!

        function countDays( startDate:Date, endDate:Date ):int
        {
            var oneDay:int = 24*60*60*1000; // hours*minutes*seconds*milliseconds
            var diffDays:int = Math.abs((startDate.getTime() - endDate.getTime())/(oneDay));
            return diffDays;
        }

        countDays( new Date( 2010, 00, 01 ), new Date( 2011, 00, 01 ) );
        // returns 365, which is correct

        countDays( new Date( 2010, 00, 01 ), new Date( 2010, 03, 01 ) );
        // returns 89, which is 1 day short

        countDays( new Date( 2010, 03, 01 ), new Date( 2010, 06, 01 ) );
        // returns 91, which is correct

        countDays( new Date( 2010, 06, 01 ), new Date( 2010, 09, 01 ) );
        // returns 92, which is correct

        countDays( new Date( 2010, 09, 01 ), new Date( 2011, 00, 01 ) );
        // returns 92, which is correct
+2  A: 

Daylight Savings, maybe? You lose an hour in the first quarter, so your function must be truncating the int instead of rounding.

uhleeka
+1  A: 

Can't be sure. I'd guess at a rounding/truncation error.

+2  A: 

Below should work:

    function countDays( startDate:Date, endDate:Date ):int 
    { 
        var oneDay:int = 24*60*60*1000; // hours*minutes*seconds*milliseconds 
        var diffDays:int = Math.round(Math.abs((startDate.getTime() - endDate.getTime())/(oneDay))); 
        return diffDays; 
    } 
John Hartsock
That coupled with uhleeka's answer (Daylight Savings Time) is where the problem is. There are 90 days in Q1, but March 14, 2010 has only 23 hours, so dividing by your oneDay would be 89.96 days. Math.floor drops you down to 89 days.
daniel.reicher
Math.ceil will not work in Q4. Due to DST, Q4 should have 92.04 days, which using Math.ceil would give you 93 days.
uhleeka
Uhleeka you are right. This one works for all cases though: Math.round(Math.abs( myExpression );
James
John I want to give you a check mark b/c you got the ball rolling and lead to the answer that worked.. Can you edit your answer to use the Math.round(Math.abs and I'll check it as the accepted answer?
James
@James: No self-promotion intended...John's answer is clearly wrong. Ball-rolling? Yes. But asking him to adjust his answer from incorrect to correct seems to defeat the purpose of stackoverflow and its award system.
uhleeka
Corrected the answer as asked. Note this is a wiki site... you can always see revisions
John Hartsock
uhleeka, sorry bout that, I guess you are right.. I am still getting used to the way this place works. What I really want is for the clearest solution to be up there with a check mark on it so if anyone else wants the answer it is right up there in bold without them having to hunt around. Thanks a lot for your help in getting to the answer though, I appreciate you all taking the time.
James