rotaready / moment-range

Fancy date ranges for Moment.js
The Unlicense
1.69k stars 202 forks source link

range.by('month') excluding last month in range #282

Open razzp opened 4 years ago

razzp commented 4 years ago

I have a date range that starts in June and runs through July and into August. Creating an iterable of months in the range yields only the first two months. August is not included.

Steps to Reproduce

const range = moment.range('2020-06-23', '2020-08-21');
const months = Array.from(range.by('months')).map(date => date.format('MMMM'));
console.log(months);

Expected Behavior

Array [ "June", "July", "August" ]

Current Behavior

Array [ "June", "July" ]


moment.js 2.27.0 moment-range 4.0.2

Unless I'm missing something this seems to be a bug. Explicitly setting { excludeEnd: false } doesn't work either.

MorenoMdz commented 4 years ago

Same thing is happening when using by "day" it is excluding the last day.

linxianxi commented 4 years ago

same question

cyonkee commented 2 years ago

I removed the module because of this issue. The following works fine:

private getDateRange(from: Moment, to: Moment): readonly Moment[] {
    const numberOfDays = to.diff(from, 'days') + 1;
    return [...Array(numberOfDays).keys()].map(i => moment(from).add(i, 'days'));
}