获取javascript 过去12个月的数组功能

//获取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())

获取javascript 过去12个月的数组功能

上一篇:python按目录名删除7天前的目录


下一篇:java中date获取前一天和后一天