给定开始/结束月份,如何生成这个时间区间内的详细月份数组
菜鸟求大佬解答,很着急
`
var startTime = "2020-11";
var endTime = "2021-02";
已知起/终月份,如何得到瞎下面这个数据
arr = ["2020-11", "2020-12", "2021-01", "2021-02"];
`
function zeroPrefix(val) {
return (val + '').padStart(2, '0');
}
function getTimeInterval() {
const [startYear, startMonth] = startTime.split('-');
const [endYear, endMonth] = endTime.split('-');
const startTimestamp = new Date(startYear, startMonth - 1).getTime();
const endTimestamp = new Date(endYear, endMonth - 1).getTime();
const intervals = [];
let timestamp = startTimestamp;
while (timestamp <= endTimestamp) {
const now = new Date(timestamp);
const year = now.getFullYear();
const month = now.getMonth() + 1;
intervals.push(`${year}-${zeroPrefix(month)}`);
timestamp = new Date(year, month).getTime();
}
return intervals;
}