1

I'm trying to populate an array with a list of dates.

When I go back to retrieve a date in the array, they are all the same date.

var paydates = new Array();
var i;
var firstDate = new Date();
firstDate.setFullYear(2010, 11, 26); //set the first day in our array as 12/26/10

for (i=0; i < 200; i++) //put 200 dates in our array
{
    paydates[i] = firstDate;
    firstDate.setDate(firstDate.getDate()+14); //add 14 days
    document.write("1st: " + i + ":" + paydates[i] + "<br />");
    //this lists the dates correctly
}

//when I go back to retrieve them:
for (i=0; i < 200; i++)
{
    document.write("2nd: " + i + ":" + paydates[i] + "<br />");
    //this lists 200 instances of the same date
}

It's probably something stupid, but I'm at a loss.

Thanks

0

3 Answers 3

6

In your loop, you assign paydates[i] a reference to firstDate. At the end of 200 iterations, all 200 locations in the paydates array are pointing to the last firstDate.

You should create a new Date instance in each iteration and then assign it to an index in the paydates array.

Also, you'll notice that the first date listed in your example is not 12/26/2010, but 1/9/2011. I'm not sure if that's a mistake or intentional, but as your code is, the first firstDate isn't the date you used to seed your array of dates.

JSFiddle of a working example that also simplifies your code a little bit. Here is the barebones code from the fiddle:

var paydates = []; // new array
var firstDate = new Date(2010, 11, 26); // seed date

for (var i = 0; i < 200; i++) {
    paydates.push(new Date(firstDate.getTime()));
    firstDate.setDate(firstDate.getDate() + 14); // add 14 days
}
Sign up to request clarification or add additional context in comments.

Comments

3

Replace the first loop with:

var temp;

for (i=0; i < 200; i++) //put 200 dates in our array
{
    temp = new Date(firstDate.getTime());
    paydates[i] = temp;
    firstDate.setDate(firstDate.getDate()+14); //add 14 days
    document.write("1st: " + i + ":" + paydates[i] + "<br />");
}

The problem was that you were storing a reference to your one and only firstDate object to each index in your array.

4 Comments

I would declare var temp; outside of the for loop. JavaScript is function-scoped, so that's what it comes down to, anyway. Better to be explicit about it.
Yes, var temp is better declared outside at the start of the function. It will actually save the run-time the hassle of ignoring the redeclaration every time. Answer edited.
How about forgetting about temp and using paydates[i] = new Date(firstDate.getTime()); as the first line in the for-loop?
@Cory: I was just about to post a new answer to suggest that, but I see you beat me to it in your latest edit. :-)
2

You must create a new Date based on firstDate to get individual date to each element in array

Instead:

paydates[i] = firstDate;
firstDate.setDate(firstDate.getDate()+14); //add 14 days

write:

paydates[i] = new Date( firstDate.setDate(firstDate.getDate()+14));

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.