The problem of finding the difference in days between two dates

I use the neat routine I found here to calculate the difference in days between two dates in AS3. I get some strange results, and I wonder if any of you can intervene in the internode-mega-lords to shed light?

Why does Q1 2010 come in a short day, when in all other cases the procedure is performed normally?

Many thanks in advance to everyone 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 
+4
source share
3 answers

The following 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; } 
+4
source

Summer time maybe? You lose an hour in the first quarter, so your function should trim int instead of rounding.

+4
source

Not sure. I would prefer a rounding / truncation error.

+1
source

Source: https://habr.com/ru/post/1312371/


All Articles