-
-
Save oshanz/2a7f1b6c0cb1b281c407 to your computer and use it in GitHub Desktop.
Get a list of dates between two dates using javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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