//获取12个月数组
function getLast12Months(){
var last12Months = [];
var today = new Date();
var year = today.getFullYear();
today.setMonth(today.getMonth());
for(var i=0;i<12;i++){
last12Months[11-i] = today.getFullYear() + fillZero(Number(today.getMonth())+1,2);//这里+1是因为js 的月份是0-11
if(today.getMonth == 0){
today.setFullYear(Number(year)-1);
}
today.setMonth(Number(today.getMonth()-1));
}
return last12Months;
}
//补零操作,保证月份为1位的时候补充前置0
function fillZero(num,fill){
var len = ('' + num).length;
return (Array(fill > len ? fill - len + 1 || 0 : 0).join(0) + num);
}
console.log(getLast12Months())