方法1:
//当前月份上一个月
getLastMonth() {
let currentDate = new Date();
let last_month = currentDate.getMonth() - 1;
if (last_month < 0) {
currentDate.setMonth(11);
} else if (last_month == 0) {
currentDate.setMonth(0);
} else {
currentDate.setMonth(last_month);
}
this.query.time = currentDate;
},
方法2:
const getHandledValue = num => {
return num < 10 ? '0' + num : num
}
const getNowMonth = () => {
const date = new Date()
const year = date.getFullYear()
const month = getHandledValue(date.getMonth() + 1)
const nowMonth = year + "-" + month
return nowMonth
}
export const getPreMonth = () => {
const arr = getNowMonth().split("-")
const year = arr[0]
const month = arr[1]
let year2 = year
let month2 = parseInt(month) - 1
if (month2 == 0) {
year2 = parseInt(year2) - 1
month2 = 12
}
if (month2 < 10) {
month2 = "0" + month2
}
const preMonth = year2 + "-" + month2
return preMonth
}