Issue with Date object in JavaScript?

I’m working on a widget which calculates a new date using this code:

var today = new Date();
var nextDay = new Date();

for (var day = 0; day < 4; day++) {
nextDay.setDate(today.getDate() + day);
console.log("today: " + today + "\nnextday: " + nextDay + "\n");
}

On May 30th 2019 it gives this console output:

today: Thu May 30 2019 09:57:53 GMT+0200 (Mitteleuropäische Sommerzeit) nextday: Thu May 30 2019 12:57:53 GMT+0200 (Mitteleuropäische Sommerzeit)

today: Thu May 30 2019 09:57:53 GMT+0200 (Mitteleuropäische Sommerzeit) nextday: Fri May 31 2019 12:57:53 GMT+0200 (Mitteleuropäische Sommerzeit)

today: Thu May 30 2019 09:57:53 GMT+0200 (Mitteleuropäische Sommerzeit) nextday: Sat Jun 01 2019 12:57:53 GMT+0200 (Mitteleuropäische Sommerzeit)

today: Thu May 30 2019 09:57:53 GMT+0200 (Mitteleuropäische Sommerzeit) nextday: Wed Jul 03 2019 12:57:53 GMT+0200 (Mitteleuropäische Sommerzeit)

Is this an error in the Date object, or is it not fine to calculate a new date that is crossing a month border by more than one day?

Seems this is really a JavaScript issue. I did some testing on the MDN page:

var date1 = new Date();
var date2 = new Date();

date1.setDate(date2.getDate() + 3);
console.log("Direct increment by 3 days:");
console.log(date1);
console.log("fine.");

var date1 = new Date();
var date2 = new Date();

console.log("Increment by 0,1,2,3 days with freshly initialized dates:");
date1.setDate(date2.getDate() + 0);
console.log(date1);
console.log("fine.");
date1.setDate(date2.getDate() + 1);
console.log(date1);
console.log("fine.");
date1.setDate(date2.getDate() + 2);
console.log(date1);
console.log("fine.");
date1.setDate(date2.getDate() + 3);
console.log(date1);
console.log("Weird.");

var date1 = new Date();
var date2 = new Date();

console.log();
console.log("Now without reinitializing the dates:");
date1.setDate(date2.getDate() + 3);
console.log("Direct increment by 3 days:");
console.log(date1);
console.log("fine.");


console.log("Increment by 0,1,2,3 days:");
date1.setDate(date2.getDate() + 0);
console.log(date1);
console.log("Weird.");
date1.setDate(date2.getDate() + 1);
console.log(date1);
console.log("Weird.");
date1.setDate(date2.getDate() + 2);
console.log(date1);
console.log("Weird.");
date1.setDate(date2.getDate() + 3);
console.log(date1);
console.log("Weird.");

Output:
`> “Direct increment by 3 days:”

Sun Jun 02 2019 10:50:14 GMT+0200 (Mitteleuropäische Sommerzeit)
“fine.”
“Increment by 0,1,2,3 days with freshly initialized dates:”
Thu May 30 2019 10:50:14 GMT+0200 (Mitteleuropäische Sommerzeit)
“fine.”
Fri May 31 2019 10:50:14 GMT+0200 (Mitteleuropäische Sommerzeit)
“fine.”
Sat Jun 01 2019 10:50:14 GMT+0200 (Mitteleuropäische Sommerzeit)
“fine.”
Wed Jul 03 2019 10:50:14 GMT+0200 (Mitteleuropäische Sommerzeit)
“Weird.”

“Now without reinitializing the dates:”
“Direct increment by 3 days:”
Sun Jun 02 2019 10:50:14 GMT+0200 (Mitteleuropäische Sommerzeit)
“fine.”
“Increment by 0,1,2,3 days:”
Sun Jun 30 2019 10:50:14 GMT+0200 (Mitteleuropäische Sommerzeit)
“Weird.”
Mon Jul 01 2019 10:50:14 GMT+0200 (Mitteleuropäische Sommerzeit)
“Weird.”
Thu Aug 01 2019 10:50:14 GMT+0200 (Mitteleuropäische Sommerzeit)
“Weird.”
Mon Sep 02 2019 10:50:14 GMT+0200 (Mitteleuropäische Sommerzeit)
“Weird.”`

I think you want this:

var today = new Date();

for (var day = 0; day < 4; day++) {
  tempDay = new Date();
  tempDay.setDate(today.getDate() + day);
  console.log("today: " + today + "\nnextday: " + tempDay + "\n");
}

Output:

today: Fri May 31 2019 20:15:59 GMT-0400 (Eastern Daylight Time)
nextday: Fri May 31 2019 20:15:59 GMT-0400 (Eastern Daylight Time)

today: Fri May 31 2019 20:15:59 GMT-0400 (Eastern Daylight Time)
nextday: Sat Jun 01 2019 20:15:59 GMT-0400 (Eastern Daylight Time)

today: Fri May 31 2019 20:15:59 GMT-0400 (Eastern Daylight Time)
nextday: Sun Jun 02 2019 20:15:59 GMT-0400 (Eastern Daylight Time)

today: Fri May 31 2019 20:15:59 GMT-0400 (Eastern Daylight Time)
nextday: Mon Jun 03 2019 20:15:59 GMT-0400 (Eastern Daylight Time)

Thanks.