1.yyyy-mm-dd获取上个月
function getUpMonth(t){ var tarr = t.split('-'); var year = tarr[0]; //获取当前日期的年 var month = tarr[1]; //获取当前日期的月 var day = tarr[2]; //获取当前日期的日 var days = new Date(year,month,0); days = days.getDate();//获取当前日期中的月的天数 var year2 = year; var month2 = parseInt(month)-1; if(month2==0) { year2 = parseInt(year2)-1; month2 = 12; } var day2 = day; var days2 = new Date(year2,month2,0); days2 = days2.getDate(); if(day2>days2) { day2 = days2; } if(month2<10) { month2 = '0'+month2; } var t2 = year2+'-'+month2+'-'+day2; return t2; }
2.yyyymm获取上个月
function getUpMonth(t){ var year =t.substring(0,4); //获取当前日期的年 var month = t.substring(4,6); //获取当前日期的月 var year2 = year; var month2 = parseInt(month)-1; if(month2==0) { year2 = parseInt(year2)-1; month2 = 12; } if(month2<10) { month2 = '0'+month2; } var m = year2.toString(); var n= month2.toString(); var t2 = m+n; return t2; }