javascript array date error -


i bit confused one.

this piece of code works has strange behavior.

var tmpcurdte = eval(datasource[i].startdate); tmpcurdte.setdate(tmpcurdte.getdate() + 1);  while (tmpcurdte < tmpenddte) {   console.log("block date: " + tmpcurdte);   blockdayarray[blockdayarray.length] = tmpcurdte;   console.log("blockdayarray: " + blockdayarray);   tmpcurdte.setdate(tmpcurdte.getdate() + 1); } 

output

block date :sat nov 12 2016 00:00:00 gmt+0100 (cet) blockdayarray :**sat nov 12** 2016 00:00:00 gmt+0100 (cet) block date :sat dec 31 2016 00:00:00 gmt+0100 (cet) blockdayarray :**sun nov 13** 2016 00:00:00 gmt+0100 (cet),**sat dec 31** 2016 00:00:00 gmt+0100 (cet) block date :sun jan 01 2017 00:00:00 gmt+0100 (cet) blockdayarray :sun nov 13 2016 00:00:00 gmt+0100 (cet),**sun jan 01 2017** 00:00:00 gmt+0100 (cet),**sun jan 01 2017** 00:00:00 gmt+0100 (cet) block date :sat feb 04 2017 00:00:00 gmt+0100 (cet) blockdayarray :sun nov 13 2016 00:00:00 gmt+0100 (cet),mon jan 02 2017 00:00:00 gmt+0100 (cet),**mon jan 02 2017** 00:00:00 gmt+0100 (cet),sat feb 04 2017 00:00:00 gmt+0100 (cet) 

as can see previous date in array changes when push new one. can help/explain this?

it's because you're pushing same date object onto array repeatedly, , changing state.

instead, want create new date object next day:

var tmpcurdte = eval(datasource[i].startdate); tmpcurdte.setdate(tmpcurdte.getdate()+1);  while (tmpcurdte < tmpenddte) {     console.log("block date :" + tmpcurdte);     blockdayarray[blockdayarray.length]=tmpcurdte;     console.log("blockdayarray :" + blockdayarray);     tmpcurdte = new date(tmpcurdte.gettime());     // ***     tmpcurdte.setdate(tmpcurdte.getdate() + 1); } 

side note: i'm not quite sure you're doing call eval, there's better way whatever have doing.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -