Skip to content

Instantly share code, notes, and snippets.

@abonello
Forked from miguelmota/getDates.js
Created December 30, 2021 20:22
Show Gist options
  • Select an option

  • Save abonello/0a4d4ff86bdf1ac14e94e735fd5b218b to your computer and use it in GitHub Desktop.

Select an option

Save abonello/0a4d4ff86bdf1ac14e94e735fd5b218b 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
function getDates (startDate, endDate) {
const dates = []
let currentDate = startDate
const addDays = function (days) {
const 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
const 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