JS - 计算考虑闰年

时间:2016-09-06 08:59:48

标签: javascript date duration leap-year

我想计算两个日期之间的天数。常见问题。

例如:

var d0 = new Date("2016-02-27");
var d1 = new Date("2017-08-25");

Many people建议使用时代差异:

var res = (d1 - d0) / 1000 / 60 / 60 / 24;
// res = 545 days

但我很怀疑所以我写了一个天真的函数:

function days(d0, d1)
{
    var d = new Date(d0);
    var n = 0;
    while(d < d1)
    {
        d.setDate(d.getDate() + 1);
        n++;
    }
    return n;
}

这个函数和纪元差异基本上输出相同的结果,但不是我的特殊例子。可能是因为2016年是闰年。

res = days(d0, d1);
// res = 546 days

知道为什么吗?

2 个答案:

答案 0 :(得分:1)

经过测试,循环似乎停在2017-08-26,而不是2017-08-25

当您打印d0d1的值时,结果如下:

  

d0:2016年2月27日星期六01:00:00 GMT + 0100(中欧标准时间)

     

d1:2017年8月25日星期五02:00:00 GMT + 0200(中欧夏令时间)

正如你所看到的,两个日期之间有一个小时的转换,所以当循环的索引到2017年8月25日时,这个转变仍然存在并使“低于”操作成立,它应该在哪里是假的。

确保在使用前将日期标准化。

答案 1 :(得分:0)

它与闰年没有任何关系,而是因为你正在混淆UTC和当地时间。

new Date("2016-02-27") // JS will interpret this input as UTC due to the hyphens

d.setDate(d.getDate() + 1);  // these get/set functions work with the LOCAL date

这两者都需要在相同的环境中工作。由于您无法确定本地时区是否会遇到夏令时,或者您是否会跨越DST转换,因此使用UTC工作比在当地时间工作更安全。

只需更改您要使用的功能:

d.setUTCDate(d.getUTCDate() + 1);

或者,考虑像moment.js这样的库已经解决了这些问题。

moment("2017-08-25").diff("2016-02-27", "days")  // 545

您展示的基于纪元的方法也是正确的,只要输入被解释为UTC。

相关问题