Bootstrap

js获取近几个月的年月

let type = 5 //近n个月的
let month = new Date().getMonth() + 1 //当前月份
let year = new Date().getFullYear() //今年
for(let i=1;i<=type;i++){
  let date = ''
  if(month >= i){
    date = year + '-' + (month - i + 1)
  } else {
    date = (year-1) + '-' + (12 + month - i + 1)
  }
  console.log(date)
}

 

;