1、例如:输入起止时间,获取以下内容
2、方法如下:
getYearAndMonth(start, end) { let month; const result = []; const newResult = []; const starts = start.split('-'); const ends = end.split('-'); let staYear = parseInt(starts[0]); let staMon = parseInt(starts[1]) - 1; const endYear = parseInt(ends[0]); const endMon = parseInt(ends[1]); while (staYear <= endYear) { if (staYear === endYear) { while (staMon < endMon) { staMon++; result.push({year: staYear, month: staMon}); } staYear++; } else { staMon++; if (staMon > 12) { staMon = 1; staYear++; } result.push({year: staYear, month: staMon}); } } for(let i=0; i<result.length; i++){ const year = result[i].year; const monthinit = result[i].month; if(monthinit<10){ month = '0' + monthinit; }else{ month = monthinit + ''; } const ym = year + '-' + month; newResult.push(ym); } console.log(result); console.log(newResult); return newResult; //return 内容可以根据实际返回
}
3、结果如下: