2

I am trying to create an object with all the dates from "today" until 60 days from now.

My code:

var dates = [];
getDateRange();

function getDateRange() {

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

  for (var i = 0; i <= 59; i++) {
    date.setDate(today.getDate()+i);
    console.log(date);
    dates[i] = date;
  };

  console.log(dates);
}

Prints out the dates correctly with the "console.log(date)" command, as follows:

Mon Jun 15 2015 21:31:47 GMT+0200
Tue Jun 16 2015 21:31:47 GMT+0200
Wed Jun 17 2015 21:31:47 GMT+0200
Thu Jun 18 2015 21:31:47 GMT+0200
Fri Jun 19 2015 21:31:47 GMT+0200
...

But once it is put into the dates array and printed out I get the following array of dates:

[Wed Jan 03 2024 21:42:47 GMT+0100,
 Wed Jan 03 2024 21:42:47 GMT+0100,
 Wed Jan 03 2024 21:42:47 GMT+0100,
 Wed Jan 03 2024 21:42:47 GMT+0100,
 ...,
 Wed Jan 03 2024 21:42:47 GMT+0100 
]

I hope I explained the problem well.

The code is part of an AngularJS app but I think my problem has to do only with Javascript.

0

4 Answers 4

1

You need to reset the date, or they will be references of the same Date object.

var dates = [];
getDateRange();

function getDateRange() {
  var today = new Date();
  var date;

  for (var i = 0; i <= 59; i++) {
    date = new Date();
    date.setDate(today.getDate() + i);
    console.log(date);
    dates.push(date);
  };

  console.log(dates);
}

Sign up to request clarification or add additional context in comments.

Comments

1

In JavaScript, Date is a reference type. If you assign it to another var, the other var becomes a reference to the original date. So in this case, you end up with 59 pointers to the same date.

Solution: to actually makes copies of the date, you should clone it, by writing

dates[i] = new Date(date.getTime());

var dates = [];
getDateRange();

function getDateRange() {

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

  for (var i = 0; i <= 59; i++) {
    date.setDate(today.getDate()+i);
    console.log(date);
    dates[i] = new Date(date.getTime());
  };

  console.log(dates);
}

Comments

0

Try creating a new date every iteration instead of using the same date object:

function getDateRange() {

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

  for (var i = 0; i <= 59; i++) {
    var temp = new Date();
    temp.setDate(today.getDate()+i);
    console.log(temp);
    dates[i] = temp;
  };

  console.log(dates);
}

In your case you were assigning the same date object to every location of the array and every time you update the date object all locations get updated because they all point to the same Date object.

Comments

0

You need to assign each member of your array to a unique new Date() instance.

In your example, the console.log() call correctly outputs the new time you've set date to in each iteration of the loop... but you're just changing the same Date object. Because every index of the array references the same Date object, they all point to the last set time (hence your problem).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.