Iterate through a range of dates in Javascript

Nexo

For the last few days I was struggling with iterating through a range of dates. I was using following piece of code to test:

var current_date = new Date("2014-08-01");
var end_date = new Date("2014-10-31");
var end_date_time = end_date.getTime();

while (current_date.getTime() <= end_date_time) {
    document.write(current_date + '<br>');
    current_date.setDate(current_date.getDate()+1);
}

To me it looks correct, but there's a problem. It's missing the last day. I was turning this code around, used a for- loop, defined new Date within loop and all the things you can imagine. One thing stayed the same. Last day missing!

By curiosity I used following format to create the Dates:

var current_date = new Date("08/01/2014");
var end_date = new Date("10/31/2014");

And to my surprise, it worked as expected. Now I'm wondering if this is a normal behaviour or a bug in Date?

I would be thankfull, if someone can enlighten me.

dariogriffo

that is because there was a change in time, check that the first days are in GMT Daylight Time and the lasts in (GMT Standard Time)

so your code 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_date = new Date("2014-10-31");
end_date = new Date(end_date.getUTCFullYear(), end_date.getUTCMonth(), end_date.getUTCDate(),  end_date.getUTCHours(), end_date.getUTCMinutes(), end_date.getUTCSeconds());
var end_date_time = end_date.getTime();


while (current_date <= end_date) {
    document.write(current_date + '<br>');
    current_date.setDate(current_date.getDate()+1);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Lua Iterate Through Date Range

From Dev

javascript iterate through array

From Dev

Pandas -- how to iterate through a list of dates which filter a DataFrame

From Dev

Javascript: Iterate/loop through multiple axes/directions?

From Dev

Using range of dates to iterate through series of date stamped XHR requests

From Dev

Iterate one for in loop through multiple objects in JavaScript

From Dev

Calculate the recurring dates between a range of dates in javascript

From Dev

iterate through a model map list in javascript

From Dev

Iterate through array backwards from index in JavaScript

From Dev

Iterate over dates range (the scala way)

From Dev

Loop through range of dates in SQL Server

From Dev

Iterate through JSON object using DOJO or javascript

From Dev

Pythonic way to iterate through a range starting at 1

From Dev

JavaScript loop through Json dates

From Dev

Faster way to iterate through range

From Dev

Oracle SQL iterate through dates

From Dev

Iterate through date range - monthwise

From Dev

Iterate through the model object in javascript (foreach and <text>)

From Dev

Pandas -- how to iterate through a list of dates which filter a DataFrame

From Dev

Using range of dates to iterate through series of date stamped XHR requests

From Dev

Iterate through a range of dates in Javascript

From Dev

Iterate through an array of objects in JavaScript

From Dev

iterate through mongodb entry javascript node

From Dev

Iterate through result until 0 javascript

From Dev

Loop through range of dates in SQL Server

From Dev

iterate through on click function javascript

From Dev

How to specify range to iterate through an array in ansible

From Dev

Oracle SQL iterate through dates

From Dev

Iterate through Object keys in Javascript

Related Related

  1. 1

    Lua Iterate Through Date Range

  2. 2

    javascript iterate through array

  3. 3

    Pandas -- how to iterate through a list of dates which filter a DataFrame

  4. 4

    Javascript: Iterate/loop through multiple axes/directions?

  5. 5

    Using range of dates to iterate through series of date stamped XHR requests

  6. 6

    Iterate one for in loop through multiple objects in JavaScript

  7. 7

    Calculate the recurring dates between a range of dates in javascript

  8. 8

    iterate through a model map list in javascript

  9. 9

    Iterate through array backwards from index in JavaScript

  10. 10

    Iterate over dates range (the scala way)

  11. 11

    Loop through range of dates in SQL Server

  12. 12

    Iterate through JSON object using DOJO or javascript

  13. 13

    Pythonic way to iterate through a range starting at 1

  14. 14

    JavaScript loop through Json dates

  15. 15

    Faster way to iterate through range

  16. 16

    Oracle SQL iterate through dates

  17. 17

    Iterate through date range - monthwise

  18. 18

    Iterate through the model object in javascript (foreach and <text>)

  19. 19

    Pandas -- how to iterate through a list of dates which filter a DataFrame

  20. 20

    Using range of dates to iterate through series of date stamped XHR requests

  21. 21

    Iterate through a range of dates in Javascript

  22. 22

    Iterate through an array of objects in JavaScript

  23. 23

    iterate through mongodb entry javascript node

  24. 24

    Iterate through result until 0 javascript

  25. 25

    Loop through range of dates in SQL Server

  26. 26

    iterate through on click function javascript

  27. 27

    How to specify range to iterate through an array in ansible

  28. 28

    Oracle SQL iterate through dates

  29. 29

    Iterate through Object keys in Javascript

HotTag

Archive