Skip to content

Instantly share code, notes, and snippets.

@openqubit
Forked from miguelmota/getDates.js
Created November 4, 2016 07:59
Show Gist options
  • Save openqubit/54bf2121c8deddbe70a57772d12c0d2d to your computer and use it in GitHub Desktop.
Save openqubit/54bf2121c8deddbe70a57772d12c0d2d to your computer and use it in GitHub Desktop.
Get dates in between two dates with JavaScript.
// Returns an array of dates between the two dates
var getDates = function(startDate, endDate) {
var dates = [],
currentDate = startDate,
addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
while (currentDate <= endDate) {
dates.push(currentDate);
currentDate = addDays.call(currentDate, 1);
}
return dates;
};
// Usage
var dates = getDates(new Date(2013,10,22), new Date(2013,11,25));
dates.forEach(function(date) {
console.log(date);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment