vue获取近半年月份

 <a-form-model-item label="月份" class="input">
       <a-select placeholder="请选择月份" style="width: 160px">
          <a-select-option value="one" v-for="(item, index) in array" :key="index"> {{ item }} </a-select-option>
       </a-select>
   </a-form-model-item>

 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"]

 

posted @ 2021-12-08 17:17  小兔儿_乖乖  阅读(377)  评论(0)    收藏  举报