getMonthBetween = () => {
const startDate = moment('2021-8-17');
const endDate = moment('2022-10-01');
const allYearMonth = []; // 接收所有年份和月份的数组
while (endDate > startDate || startDate.format('M') === endDate.format('M')) {
allYearMonth.push(startDate.format('YYYY-MM'));
startDate.add(1,'month');
}
console.log('所有年份和月份------>', allYearMonth);
}
输出结果:

该博客内容展示了如何使用moment.js库,计算并输出两个日期之间(包括起始月)的所有年份和月份。代码中定义了一个getMonthBetween函数,通过while循环逐月增加起始日期直到超过结束日期,将每个月份以'YYYY-MM'格式存入数组。

7907

被折叠的 条评论
为什么被折叠?



