计算起始日期之间的中间日期,例如相求 2021-12到2022-03之间的月份,得到结果:[‘2021-12’,‘2022-1’,‘’,‘2022-2’,‘2022-3’]
// 计算起始日期的中间日期
getMonthsBetweenStartAndEnd(start, end) {
let startArr = start.split("-");
let year = parseInt(startArr[0]);
let month = parseInt(startArr[1]);
let newDate = new Date(start);
let endDate = new Date(end);
let result = [];
while (newDate.getTime() < endDate.getTime()) {
result.push(`${year}-${month}`);
if (month === 12) {
month = 0;
year++;
}
month++;
newDate = new Date(`${year}-${month}`);
}
return result;// ['2021-12','2022-1','','2022-2','2022-3']
}
this.getMonthsBetweenStartAndEnd("2021-12","2022-03")