Scott Scott - 1 year ago 94
Javascript Question

javascript - get array of dates between 2 dates

var range = getDates(new Date(), new Date().addDays(7));

I'd like "range" to be an array of date objects, one for each day between the two dates.

The trick is that it should handle month and year boundaries as well.


Answer Source
function (startDate, endDate, addFn, interval) {

 addFn = addFn || Date.prototype.addDays;
 interval = interval || 1;

 var retVal = [];
 var current = new Date(startDate);

 while (current <= endDate) {
  retVal.push(new Date(current));
  current =, interval);

 return retVal;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download