JS生成当前月份包括最近12个月内的月份
1 var last_year_month = function() { 2 var result = []; 3 for(var i = 0; i < 12; i++) { 4 var d = new Date(); 5 d.setDate(1); 6 d.setMonth(d.getMonth() - i); 7 var m = d.getMonth() + 1; 8 9 m = m < 10 ? "0" + m : m; 10 //在这里可以自定义输出的日期格式 11 result.push(d.getFullYear() + "-" + m); 12 //result.push(d.getFullYear() + "年" + m + '月'); 13 } 14 15 return result; 16 }; 17 18 var time=new Array(); 19 20 //生成前12个月日期下拉框 21 for(var allinfo = last_year_month(), i = 0; i < allinfo.length; i++) { 22 time[i]=allinfo[i]; 23 }
结果:
1 ["2019-04", "2019-03", "2019-02", "2019-01", "2018-12", "2018-11", "2018-10", "2018-09", "2018-08", "2018-07", "2018-06", "2018-05"]
人生得意须尽欢,莫使金樽空对月.