2 回答

TA貢獻(xiàn)1856條經(jīng)驗 獲得超17個贊
最里面的new Date()
總是在六月創(chuàng)建一個 Date 實例。當(dāng)您將月份日期設(shè)置為 30 時,您將日期強(qiáng)制為 6 月 30 日,而不是 5 月 30 日。
調(diào)用.setDate()
可以更改月份,但僅當(dāng)月份中的某天沒有意義時,或者更?。慊蜇?fù))或更大(如 33)。由于 30 確實是六月中的真實日子,因此月份不會改變。

TA貢獻(xiàn)1906條經(jīng)驗 獲得超10個贊
在這里,我將您的代碼修改為您想要的反應(yīng):
a = [];
a[0] = new Date();
console.log("1 Element Added: "+a.length + " - " + a.toString());
//"1 Element Added: 1 - Sun Jun 02 2019 12:13:35 GMT-0400 (Eastern Daylight Time)"
a.unshift(new Date(a[0]));
a[0].setDate(a[0].getDate()-1);
console.log("First Unshift: "+a.length + " - " + a.toString());
//"First Unshift: 2 - Sat Jun 01 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sun Jun 02 2019 12:13:35 GMT-0400 (Eastern Daylight Time)"
a.unshift(new Date(a[0]));
a[0].setDate(a[0].getDate()-1);
console.log("Second Unshift: "+a.length + " - " + a.toString());
//"Second Unshift: 3 - Fri May 31 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sat Jun 01 2019 12:13:35 GMT-0400 (Eastern Daylight Time),Sun Jun 02 2019 12:13:35 GMT-0400 (Eastern Daylight Time)"
a.unshift(new Date(a[0]));
a[0].setDate(a[0].getDate()-1);
console.log("Third Unshift: "+a.length + " - " + a.toString());
添加回答
舉報