1

I'm newbie in javascript and need a little help.

How to generate all dates between dateA to dateB.

For example:

dateA = 07/01/2013
dateB = 07/01/2014

Wanted result:

07/01/2013, 07/02/2013, 07/03/2013, 07/04/2013...and so on

Any help would be greatly appreciated :)

Newbie
  • 152
  • 2
  • 13
  • possible duplicate of [javascript - get array of dates between 2 dates](http://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates) – 0not Jul 11 '14 at 22:05
  • possible duplicate of [Simple javascript date math... not really](http://stackoverflow.com/questions/6356164/simple-javascript-date-math-not-really) – PM 77-1 Jul 11 '14 at 22:06
  • You can try using momentjs.com – Vikram Tiwari Jul 11 '14 at 22:21

1 Answers1

4

Javascript doesn't have the simplest library for dealing with dates. Particularly when it comes to adding dates. One common method is to convert the date object into its representation in seconds using getTime() then to add the required number of seconds and pass that result into the new Date method. Something like this:

var dateA = new Date(2014,6,1,0,0,0);
var dateB = new Date(2014,6,4,0,0,0);
for(var myDate = dateA; myDate <= dateB; myDate = new Date(myDate.getTime() + 1000 * 60 * 60 * 24))
{
    var formatedDate = myDate.getMonth()+1;
    formatedDate += "/" + myDate.getDate() + "/" + myDate.getFullYear();
    console.log(formatedDate);
}

Also remember that in javascript months are zero indexed (0-11).

Joel Anderson
  • 535
  • 8
  • 22