Skip to content

Instantly share code, notes, and snippets.

@stekhn
Last active October 29, 2020 15:37
Show Gist options
  • Save stekhn/994fb1ef3540263c4df5d858252f6029 to your computer and use it in GitHub Desktop.
Save stekhn/994fb1ef3540263c4df5d858252f6029 to your computer and use it in GitHub Desktop.
Create a range of dates between two dates. The step parameter defines the interval between the individual dates.
function dateRange(startDate, endDate, steps = 1) {
const dateArray = [];
let currentDate = new Date(startDate);
while (currentDate <= new Date(endDate)) {
dateArray.push(new Date(currentDate));
// Use UTC date to prevent problems with time zones and DST
currentDate.setUTCDate(currentDate.getUTCDate() + steps);
}
return dateArray;
}
// const dates = dateRange('2020-09-27', '2020-10-28', 7);
// console.log(dates);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment