Creates an generator to lazily loop over a date range using moment.
- Node >= 8
- moment (Peer dependency)
If you use Typescript, Typescript 3.6+ is required since this lib uses the global generic Generator type.
$ npm install @simonja/moment-date-range --save
import {range} from '@simonja/moment-date-range';
const generator = range(moment('2020-01-21'));
for (const day of generator) {
console.log(day.format('YYYY-MM-DD')) // 2020-01-21 -> Infinity
}
import {range} from '@simonja/moment-date-range';
const generator = range(moment('2020-01-21'), moment('2020-01-31'));
for (const day of generator) {
console.log(day.format('YYYY-MM-DD')) // 2020-01-21 -> 2020-01-31
}
import {range} from '@simonja/moment-date-range';
const generator = range(moment('2020-01-01'), moment('2020-01-05'), {step: 2, unit: 'd'});
for (const day of generator) {
console.log(day.format('YYYY-MM-DD')) // 2020-01-01 -> 2020-01-03 -> 2020-01-05
}
import {range} from '@simonja/moment-date-range';
const generator = range(moment('2020-01-01'), moment('2020-01-10'));
const dates = Array
.from(generator)
.map(item => item.format('YYYY-MM-DD'));
console.log(dates)
// ['2020-01-01',
// '2020-01-02',
// '2020-01-03',
// '2020-01-04',
// '2020-01-05',
// '2020-01-06',
// '2020-01-07',
// '2020-01-08',
// '2020-01-09',
// '2020-01-10']
import {range} from '@simonja/moment-date-range';
const generator = range(moment('2020-01-31'), moment('2020-01-29'), {step: -1});
for (const day of generator) {
console.log(day.format('YYYY-MM-DD')) // 2020-01-31 -> 2020-01-30 -> 2020-01-29
}
import {range} from '@simonja/moment-date-range';
const generator = range(moment('2020-01-01'), moment('2020-01-18'), {unit: 'w'});
for (const day of generator) {
console.log(day.format('YYYY-MM-DD')) // 2020-01-01 -> 2020-01-08 -> 2020-01-15
}
Type: moment
object
Start of the date range
Type: moment
object
End of the date range
Configuration options
Number of units between each iteration. Default 1
Unit used during iteration for comparison and cycling. Default d
Result of the range
function. Returns a generator that lazily creates moment
objects.