let data=new Date();
let aa=data.getFullYear(); //2023
var b=[];
for (let i = 5; i > 0; i--) {
let temp=aa-i;
console.log(temp);
b.push(temp)
}
for (let i = 0; i < 6; i++) {
let temp=aa+i;
console.log(temp);
b.push(temp)
}
console.log(b) // [2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028]
简易写法
let date = new Date();
let startYear = date.getFullYear()-n;//起始年份
let endYear = date.getFullYear()+m;//结束年份
let yearList = [];
for (var i=startYear;i<=endYear;i++) {
let obj = {
name: i,
value: i+"年",
}
yearList.push(obj);
}
console.log(yearList);