Skip to content

Instantly share code, notes, and snippets.

@oshanz
Created October 2, 2014 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oshanz/2a7f1b6c0cb1b281c407 to your computer and use it in GitHub Desktop.
Save oshanz/2a7f1b6c0cb1b281c407 to your computer and use it in GitHub Desktop.
Get a list of dates between two dates using javascript
function generateDateList(from, to) {
var getDate = function(date) {//Mysql Format
var m = 1 + date.getMonth(), d = date.getDate();
return date.getFullYear() + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
}
var fs = from.split('-'), startDate = new Date(fs[0], parseInt(fs[1]) - 1, fs[2]), result = [getDate(startDate)], start = startDate.getTime(), ts, end;
if ( typeof to == 'undefined') {
end = new Date().getTime();
} else {
ts = to.split('-');
end = new Date(ts[0], parseInt(ts[1]) - 1, ts[2]).getTime();
}
while (start < end) {
start += 86400000;
startDate.setTime(start);
result.push(getDate(startDate));
}
return result;
}
console.log(generateDateList('2014-2-27', '2014-3-2'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment