JS - 考虑闰年计算两个日期之间的天数

JS - Calculate number of days between 2 dates considering 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;
}

这个函数和 epoch 的区别本质上输出相同的结果,但与我的特定示例不同。可能是因为2016年是闰年吧

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

知道为什么吗?

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

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

d0: Sat Feb 27 2016 01:00:00 GMT+0100 (Central Europe Standard Time)

d1: Fri Aug 25 2017 02:00:00 GMT+0200 (Central Europe Daylight Time)

如您所见,两个日期之间有一个小时的偏移,因此当循环的索引到达 2017 年 8 月 25 日时,这个偏移仍然存在并使“低于”操作为真,其中应该是假的。

确保在使用前规范化您的日期。

它与闰年没有任何关系,而是因为您将 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,您展示的基于纪元的方法也是正确的。