vue获取近半年月份
"月份" class="input"> select placeholder="请选择月份" style="width: 160px"> select>select-option value="one" v-for="(item, index) in array" :key="index"> {{ item }} select-option>
data中声明 array:[ ]
let nowDate = new Date() this.array = [] let year = nowDate.getFullYear() let mon = nowDate.getMonth() + 1 for (let i = 0; i < 12; i++) { mon = mon - 1 if (mon <= 0) { year = year - 1 mon = mon + 12 } if (mon < 10) { mon = '0' + mon } this.array[i] = year + '-' + mon } console.log(this.array) //["2021-12", "2021-11", "2021-10", "2021-09", "2021-08", "2021-07"]