简体   繁体   中英

store date objects to array

I have two dates and want to save the days in between (plus start and end date) in an array

    var date1 = new Date("Sep 23, 2013 12:00:00"); // Monday
    var date2 = new Date("Sep 26, 2013 12:00:00"); // Thursday

    var alldates = [];

    for (var i=0; date1 <= date2; date1.setDate(date1.getDate() + 1), i++) {
        alldates.push(date1);
        }

    alert(alldates.join('\n'));

With this code alert(alldates.join('\\n')); shows the following

    Fri Sep 27 2013 12:00:00 GMT+0200
    Fri Sep 27 2013 12:00:00 GMT+0200
    Fri Sep 27 2013 12:00:00 GMT+0200
    Fri Sep 27 2013 12:00:00 GMT+0200

I am new to Javascript and want to get further understanding, so thank you for any explanation why the alert does not show

    Mon Sep 23 2013 12:00:00 GMT+0200
    Tue Sep 24 2013 12:00:00 GMT+0200
    Wed Sep 25 2013 12:00:00 GMT+0200
    Thu Sep 26 2013 12:00:00 GMT+0200

The problem you have is that you push references to the date1 object. When you change the date on it in your loop, you update the object, but all references still point to the same object.

You need to either push a text representation of your date, or a copy of your date1 object

for (var i=0; date1 <= date2; date1.setDate(date1.getDate() + 1), i++) {
    alldates.push(new Date(date1));
    }

alert(alldates.join('\n'));

As suggested, with a while loop

while( date1 <= date2 ) {
  alldates.push(new Date(date1));
  date1.setDate( date1.getDate() +1 );
} 

Your array is storing the references for the single date object and everytime when setDate is called each of them are getting updated with new date value.

So it will be better to push the new date object in array like this,

var date1 = new Date("Sep 23, 2013 12:00:00"); // Monday
var date2 = new Date("Sep 26, 2013 12:00:00"); // Thursday

var alldates = [];
// pushing first date
alldates.push(new Date(date1.setDate(date1.getDate())));

for (var i=0; date1 <= date2; i++) {

    alldates.push(new Date(date1.setDate(date1.getDate() + 1)));
}

alert(alldates.join('\n'));

To echo the other answers, the issue is that the element being pushed to the array isn't a value which stays the same - it refers to a Date object which changes in the loop, so all references to date1 stored in alldates are set to the final value of date1 .

The accepted answer does the job, but it also changes the value of date1 . The OP set up the code this way, which indicates that this isn't a problem for them. But if you'd prefer to not mutate date1 , here's an alternative approach:

 const date1 = new Date("Sep 23, 2013 12:00:00"); // Monday const date2 = new Date("Sep 26, 2013 12:00:00"); // Thursday const alldates = Array .from(Array(date2.getDate() - date1.getDate() + 1), () => new Date()) .map((el, ind) => new Date(el.setTime(date1.getTime() + 1000 * 60 * 60 * 24 * ind))); console.log(alldates.join("\\n")); 

EXPLANATION: This first creates an array of sufficient size to store the start date, end date, and days in between, and populates the array with Date objects. Next, map applies a function to each element in the array, and this function uses the index of each successive value multiplied by the number of milliseconds in a day to produce the desired result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM