这真的是JAVASCRIPT中的一个错误吗?还是我对Date,数据类型做了什么错误



let d = new Date('2018,7,26');
let c = new Date()
c.setDate(d.getDate() + 1)
document.write(c);
document.write("<br>");
c.setDate(d.getDate() + 5)
document.write(c);
document.write("<br>");
c.setDate(d.getDate() + 6)
document.write(c);
document.write("<br>");
c.setDate(d.getDate() + 7)
document.write(c);
document.write("<br>");
c.setDate(d.getDate() + 8)
document.write(c);
document.write("<br>");
c.setDate(d.getDate() + 9)
document.write(c);
document.write("<br>");
c.setDate(d.getDate() + 10)
document.write(c);
document.write("<br>");

运行此代码的输出:

Fri Jul 27 2018 14:35:17 GMT+0530 (India Standard Time)
Tue Jul 31 2018 14:35:17 GMT+0530 (India Standard Time)
Wed Aug 01 2018 14:35:17 GMT+0530 (India Standard Time)
Sun Sep 02 2018 14:35:17 GMT+0530 (India Standard Time)
Thu Oct 04 2018 14:35:17 GMT+0530 (India Standard Time)
Sun Nov 04 2018 14:35:17 GMT+0530 (India Standard Time)
Thu Dec 06 2018 14:35:17 GMT+0530 (India Standard Time)

这看起来像一个bug。如果今天是2018-07-26(即7月26日(,那么加上7天应该是2018-08-02(8月2日(。但是,现在是九月二号。然后,进一步添加会使情况变得更糟。

如何解决这个问题?如何在JS中正确增加日期?

当您将c设置为大于月份中的天数时,它会增加c中的月份。您的代码的问题是,您没有清除c的值,并且随着每个参数>31setDate,它在c中的当前月份不断增加。

如MDN 所述

如果dayValue在该月的日期值范围之外,setDate((将相应地更新Date对象。例如,如果0为dayValue提供,则日期将设置为上个月。

如果一个月有31天,并且您将日期设置为33,则它将转到下一个月+2,因此在这种情况下为8月2日。

这里的问题是,如果你再次添加34天,它将停留在它在上一个setDate()增加到的月份,并再次添加一个月+它拥有的天数。(8月有31天,所以将定在第3天(。

举个例子:

// let d = new Date('2018,7,26');
// d.getDate will always return 26
let c = new Date();
c.setDate(27); // works fine, month has 31 days
c.setDate(31); // same
c.setDate(32); // will go to next month + 1, so august 1st
c.setDate(33); // we're at august, august has 31 days so august + 1 month + 2 days = september 2nd
c.setDate(34); // september has 30 days goes to October + 4 days = October 4th

要正确添加天数,您可以克隆日期:

let d = new Date('2018-07-26');
let c = new Date()
let e;
e = new Date(c.getTime())
e.setDate(d.getDate() + 1);
console.log(e);
e = new Date(c.getTime())
e.setDate(d.getDate() + 5);
console.log(e);
e = new Date(c.getTime())
e.setDate(d.getDate() + 6);
console.log(e);
e = new Date(c.getTime())
e.setDate(d.getDate() + 7);
console.log(e);
e = new Date(c.getTime())
e.setDate(d.getDate() + 8);
console.log(e);
e = new Date(c.getTime())
e.setDate(d.getDate() + 9);
console.log(e);
e = new Date(c.getTime())
e.setDate(d.getDate() + 10);
console.log(e);

还请注意,正如Deceze在评论中提到的那样,您用于创建日期的日期格式并不适用于所有浏览器。不如使用类似2018-07-26的东西。

JS中没有错误。只是你误解了你的代码是如何工作的。请注意此代码。

c.setDate(d.getDate() + 6) // this will set the c date into 2018-08-01
document.write(c);
document.write("<br>");
c.setDate(d.getDate() + 7)
document.write(c);
document.write("<br>");

d.getDate() + 7应该会给你2018-08-02的结果。但是,您将其放入c变量中,由于之前的设置,该变量的月份设置为8。因此,月份再次递增,结果为2018-09-02,并应用于代码的其余部分。

这是获得所需结果的固定代码。

var arr = [1, 5, 6, 7, 8, 9, 10]; // you can edit this line according to your needs
for (var i = 0; i < arr.length; i++) {
let c = new Date();
c.setDate(d.getDate() + arr[i]);
document.write(c);
document.write("<br>");
}

最新更新