830 B
830 B
title, tags, firstSeen
| title | tags | firstSeen |
|---|---|---|
| dateRangeGenerator | date,function,generator,advanced | 2021-06-21T05:00:00-04:00 |
Creates a generator, that generates all dates in the given range using the given step.
- Use a
whileloop to iterate fromstarttoend, usingyieldto return each date in the range, using theDateconstructor. - Use
Date.prototype.getDate()andDate.prototype.setDate()to increment bystepdays after returning each subsequent value. - Omit the third argument,
step, to use a default value of1.
const dateRangeGenerator = function* (start, end, step = 1) {
let d = start;
while (d < end) {
yield new Date(d);
d.setDate(d.getDate() + step);
}
};
[...dateRangeGenerator(new Date('2021-06-01'), new Date('2021-06-04'))];
// [ 2021-06-01, 2021-06-02, 2021-06-03 ]