Moment:如何获取日期范围的月份数组
Moment: how to get month array for a date range
let startDate = '2022/01/1';
let endDate = '2022/05/31'
moment 是否有从上面给定的日期范围中获取以下月份数组的功能??
[
2022/01, 2022/02, 2022/03, 2022/04, 2022/05
]
非常感谢。
您可以使用 isSameOrBefore
和 add
方法来实现此目的:
const monthsBetweenDates = (startDate, endDate) => {
const now = startDate, dates = [];
while (now.isSameOrBefore(endDate)) {
dates.push(now.format('YYYY/MM'));
now.add(1, 'months');
}
return dates;
}
const fromDate = moment();
const toDate = moment().add(6, 'months');
const results = monthsBetweenDates(fromDate, toDate);
console.log(results);
let startDate = '2022/01/1';
let endDate = '2022/05/31'
moment 是否有从上面给定的日期范围中获取以下月份数组的功能??
[
2022/01, 2022/02, 2022/03, 2022/04, 2022/05
]
非常感谢。
您可以使用 isSameOrBefore
和 add
方法来实现此目的:
const monthsBetweenDates = (startDate, endDate) => {
const now = startDate, dates = [];
while (now.isSameOrBefore(endDate)) {
dates.push(now.format('YYYY/MM'));
now.add(1, 'months');
}
return dates;
}
const fromDate = moment();
const toDate = moment().add(6, 'months');
const results = monthsBetweenDates(fromDate, toDate);
console.log(results);