Last active
October 29, 2020 15:37
-
-
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.
This file contains hidden or 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 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