admin管理员组文章数量:1396098
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 ing 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
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 ing 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
Share
Improve this question
edited May 23, 2017 at 12:11
CommunityBot
11 silver badge
asked Jun 9, 2010 at 21:21
JamesJames
6418 silver badges17 bronze badges
3 Answers
Reset to default 4Below 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;
}
Daylight Savings, maybe? You lose an hour in the first quarter, so your function must be truncating the int instead of rounding.
Can't be sure. I'd guess at a rounding/truncation error.
本文标签: javascriptProblem finding the difference in days between two datesStack Overflow
版权声明:本文标题:javascript - Problem finding the difference in days between two dates - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744630441a2616505.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论