1

For a calendar i require to generate an array of dates from 2023-01-01 till 2023-01-15. I've tried to generate the array using a loop-over, however I think the code could be much cleaner.

I would've expected javascript to have an getDateArray(new Date(), new Date('2023-01-15')) function but it doesn't.

Deyan
  • 11
  • 3
  • 1
    Does this answer your question? [Javascript - get array of dates between 2 dates](https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates) – ponury-kostek Mar 30 '23 at 08:55

1 Answers1

0

i came up with the following but don't have the reputation to post it anywhere else.

you can use the third parameter as an interval

function arrayDateRange(start, stop, step) {
    if (step < 1000) return [start, stop];
    return Array.from({ length: (stop - start) / step + 1 }, (value, index) => {
        return new Date(start.getTime() + index * step)
    });
}

arrayDateRange(new Date('2023-01-01'),new Date('2023-01-15'),(1000*60*60*24))
Deyan
  • 11
  • 3