简体   繁体   中英

Javascript Array unshift new Date subtracting 1 Day

I am getting some strange results from the following code:

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(new Date(new Date().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(new Date(new Date().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(new Date(new Date().setDate(a[0].getDate() - 1))));
console.log("Third Unshift: "+a.length + " - " + a.toString());

//"Third Unshift: 4 - Sun Jun 30 2019 12:13:35 GMT-0400 (Eastern Daylight Time),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)"

Same code works 1st and 2nd time, but the 3rd run gives an unexpected result - it should be Thu May 30 2019, not Jun 30 2019 Can someone tell me what I'm doing wrong here?

Thanks in Advance.

The innermost new Date() always makes a Date instance in June. When you set the day-of-month to 30, you're forcing the date to June 30th, not May 30th.

Calling .setDate() can change the month, but only when the day-of-month is something that doesn't make sense, either smaller (zero or negative) or bigger (like 33). Since 30 is indeed a real day in June, the month doesn't change.

@Pointy and @Titus have already explained why the code does not work as you expected. Here I leave your code modified to react as you wanted:

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM