0

I need to increment between a range of dates using javascript. I have start_date and end_date in the following format.

var start_date = Fri Nov 09 2012
var end_date = Thu Nov 15 2012

I need to call a function which accepts all the dates by looping between start dates and end dates as parameter. Something like this

for (date = start_date; date < end_date; date++) {
    getCellFromDate(date, calInstance);
}

I need to provide the parameters to getCellFromDate in the same date format(Fri Nov 09 2012) . How do I achieve this..I am new to javascript.. Please help

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
rubyist
  • 3,074
  • 8
  • 37
  • 69

3 Answers3

0

To do the iteration in pure JavaScript, without the aid of any other date library, you can change

date++

to

date = new Date(date.getTime() + 86400000)

That will go to the next day at the same time. There are 86400000 milliseconds in a day.

This will iterate through date objects. You will need to use a date formatter and parser to go back and forth between date objects and strings. There are many ways to do this. See this SO question for help.

Community
  • 1
  • 1
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
0
date.setDate(date.getDate() + 1);

Works for any number of days, negative and positive:

function addDays(date, count) {
    date.setDate(date.getDate() + count);
}
Stefan
  • 4,166
  • 3
  • 33
  • 47
0

Its better to use some date libraries. It helps you in more functions some advanced and commonly used date libraries are

Zahid Riaz
  • 2,879
  • 3
  • 27
  • 38