根据当前时间获取上个月月份日期和下个月月份日期。
// 上个月
const getPreMonth = date => {
var [year, month, day] = date.split('-'); // 分割数组获取年月日
var oldDays = new Date(year, month, 0);
oldDays = oldDays.getDate(); //获取当前日期中月的天数
var newYear = year;
var newMonth = parseInt(month) - 1;
if (newMonth == 0) {
newYear = parseInt(newYear) - 1;
newMonth = 12;
}
var newDay = day;
var newDays = new Date(newYear, newMonth, 0);
newDays = newDays.getDate();
if (newDay > newDays) {
newDay = newDays;
}
if (newMonth < 10) { // 小于10补0
newMonth = '0' + newMonth;
}
var newDates = newYear + '-' + newMonth + '-' + newDay;
return newDates;
};
const getNextMonths = date => {
var [year, month, day] = date.split('-'); // 分割数组获取年月日
var oldDays = new Date(year, month, 0);
oldDays = oldDays.getDate(); //获取当前日期中月的天数
var newYear = year;
var newMonth = parseInt(month) + 1;
if (newMonth == 13) {
newYear = parseInt(newYear) + 1;
newMonth = 1;
}
var newDay = day;
var newDays = new Date(newYear, newMonth, 0);
newDays = newDays.getDate();
if (newDay > newDays) {
newDay = newDays;
}
if (newMonth < 10) {
newMonth = '0' + newMonth;
}
var newDates = newYear + '-' + newMonth + '-' + newDay;
return newDates;
};