获取当前月份的前一个月
作者:互联网
getLastMonth(date) {
var now = new Date(date)
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var nowMonthDay = new Date(year, month, 0).getDate(); // 当前月的总天数
if (month - 1 <= 0) {
day = day < 10 ? ('0' + day) : day
return year - 1 + "-" + 12 + "-" + day
}; // 如果是1月,年数往前推一年
var lastMonthDay = new Date(year, parseInt(month) - 1, 0).getDate();
if (lastMonthDay >= day) {
month = month -1;
month = month < 10 ? ('0' + month) : month
day = day < 10 ? ('0' + day) : day
return year + "-" + month + "-" + day
};
if (day < nowMonthDay) {
month = month -1;
month = month < 10 ? ('0' + month) : month
day = lastMonthDay - (nowMonthDay - day);
day = day < 10 ? ('0' + day) : day
return year + "-" + month + "-" + day
}; // 1个月前所在月的总天数小于现在的天日期
month = month -1;
month = month < 10 ? ('0' + month) : month
lastMonthDay = lastMonthDay < 10 ? ('0' + lastMonthDay) : lastMonthDay
return year + "-" + month + "-" + lastMonthDay; // 当前天日期小于当前月总天数
},
标签:10,var,month,获取,一个月,当前,year,lastMonthDay,day 来源: https://www.cnblogs.com/lilulin/p/16496627.html