0

Question

I am attempting to build an array between two JS objects. It appears that my objects are being created correctly, and in fact that the code below is running.

The unexpected behavior is that every object in my output array is transforming to match the last date that I looped through. i.e. if I loop, whatever my todate_dateobjis, I get an entire array of just that value.

I have to do some debugging wrt the actual start/end dates being correct, but I can handle that -- what I'm stymied by is the behavior described above.

I am very new to javascript. I imagine this is some issue with mutation? Any guidance would be appreciated.

I left the console logs in just because why take them out?

Code

function build_dateobjs_array(fromdate_dateobj, todate_dateobj) {
    // return an array of dateojects from fromdate to todate
    var current_date = fromdate_dateobj;
    var return_array = []

    while (current_date <= todate_dateobj) {
        return_array[return_array.length] = current_date; // I have read that this is generally faster that arr.push()
        var tomorrow = new Date(current_date.getTime() + 86400000);
        console.log('tomorrow: ', tomorrow);
        current_date.setTime(tomorrow);
        console.log('current_date: ', current_date)
        console.log("build_dateobjs_array : ", return_array);
    };

    return return_array;
};
4
  • "I have read that this is generally faster that arr.push()" 1. Probably not, cross-browser. 2. Even if it were, would it really matter? Are you doing this hundreds of thousands of times in a loop? But yes, it's a perfectly acceptable way to add to an array. Commented Aug 16, 2015 at 22:54
  • BTW, not all days are 24 hours long, some are one hour less and some one hour more so depending on your Date object, var tomorrow = new Date(current_date.getTime() + 86400000) may not give the result you expect around daylight saving changes. Consider: tomorrow = new Date(+current_date); tomorrow.setDate(tomorrow.getDate()+1); Commented Aug 16, 2015 at 23:00
  • Thanks RobG. Good point. I'm only storing about a month's worth of data, and they're all set to noon, so that shouldn't be an issue. re speed. Yes, but what the heck? haha here's a post -- it's faster on almost all browsers Commented Aug 16, 2015 at 23:08
  • @BrandonHastings—there are many, many tests on jsperf for push vs direct assignment. Commented Aug 16, 2015 at 23:36

1 Answer 1

4

Date objects are mutable. This line:

current_date.setTime(tomorrow);

...changes the state of the Date object that current_date refers to, which you never change.

So you're storing the same object repeatedly in return_array. Instead, make a copy of the Date:

return_array[return_array.length] = new Date(+current_date);

Also, it's probably best to change

    var current_date = fromdate_dateobj;

to

    var current_date = new Date(+fromdate_dateobj);

so you're not modifying the Date that was passed in.


Side note: There's no need for the round-trip to milliseconds, simply:

function build_dateobjs_array(fromdate_dateobj, todate_dateobj) {
    // return an array of dateojects from fromdate to todate
    var current_date = new Date(+fromdate_dateobj);
    var return_array = [];

    while (current_date <= todate_dateobj) {
        return_array[return_array.length] = new Date(+current_date);
        current_date.setDate(current_date.getDate() + 1);
    };

    return return_array;
}

(There's also no reason to put a ; at the end of a function declaration.)

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

3 Comments

Thanks! I've been reading a lot about JS's Date objects... is there no potential issue with adding a day if the day spans months/years (eg. what happens if I add a day to December 31, 2005)?
@BrandonHastings: Nope. JavaScript's Date is smart about rollovers (of milliseconds, seconds, minutes, hours, days, months, and years).
@RobG: Thanks, I've fixed it in the above. I'm glad they fixed that in ES6.

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.