需求从2020至今:
要得到[2020,2021,2022]这个下拉列表
效果:
代码:
/**
* 获取到从2020那一年开始至今的年份
*/
getYearsToNow() {
// this.yearsToNow=[2020,2021,2022]
//获取到从那一年开始
let smallYears = 2020
//获取当前时间
let date = new Date
let nowYears = date.getFullYear()
let Years = nowYears - smallYears
let arrYear = []
for (let i = 0; i <= Years; i++) {
arrYear.push(nowYears--)
}
arrYear.forEach(item => {
//下拉框的数组
this.yearsToNow.push(item)
})
},
参考: