Iterate through a range of dates in Javascript -


For the last few days I was struggling with recurrence through the range of dates. I was using the following code for the exam:

  var current_date = new date ("2014-08-01"); Var End_Data = New Date ("2014-10-31"); Var end_date_time = end_date.getTime (); While (current_date.getTime () & lt; = END_DATE_TIME) {document.write (CURRENT_DATE + '
;); Current_date.setDate (current_date.getDate () +1); }

This looks right for me, but there is a problem. This is missing on the last day. I was changing this code around, used for-loop, defined a new date within the loop, and all the things you can imagine. One thing was the same! Missed the last day

The curriculum used to create my dates with curiosity:

  at CURRENT_DATE = new date ("2014/08/01"); Var End_Data = New Date ("10/31/2014");  

And my surprise is, it worked as expected now I'm wondering if this is a normal behavior or a bug in date?

I may be thankfull if someone can enlighten me.

That's because the changes were made in time, check that on the first day GMT is in daylight time and GMT

So your code is better to use UTC

  var current_date = new date ("2014-08-01"); CURRENT_DATE = new date (current_date.getUTCFullYear), current_date.getUTCMonth (), current_date.getUTCDate (), current_date.getUTchours (), current_date.getUTCMinutes (), current_date.getUTCSeconds ()); Var End_Data = New Date ("2014-10-31"); End_date = New date (end_date.getUTCFullYear), end_date.getUTCMonth (), end_date.getUTCDate (), end_date.getUTchours (), end_date.getUTCinutes (), end_date.getUTCSeconds ()); Var end_date_time = end_date.getTime (); While (CURRENT_DATE & lt; = end_date) {document.write (CURRENT_DATE + '
;); Current_date.setDate (current_date.getDate () +1); }

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -