0

I am trying to add new dates to an array with a while loop, for some reason the first time it loops through it correctly adds it. But the second time it deletes the previous time and adds 2 of the new value.

var currentDate = new Date();
var testDate = new Date(2021, 8, 27, 0, 0, 0, 0);
var displayDate = new Date(2021, 5, 27, 0, 0, 0, 0);
var dateArray = [];

while (d.getTime() + 13 < currentDate.getTime()) {
    d.setDate(d.getDate() + 14);
    displayDate.setDate(displayDate.getDate() + 14);
    console.log(displayDate);
    dateArray.push(displayDate);
    console.log(dateArray);
}
console.log(dateArray);
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30
granty100
  • 3
  • 1
  • 1
    Where did you define d? –  Jul 12 '21 at 16:00
  • displayDate is a reference, which means that you are creating an array with two references to the same object. You need to do `dateArray.push(new Date(displayDate.valueOf()))` – Charlie Bamford Jul 12 '21 at 16:01

0 Answers0