js 获取下一个月时间和下一年的时间

 

// d 实例 :2025-09-01

getNextMonthDate(d) {
let t = d.split("-");
console.log(t)
let year = parseInt(t[0]);
let month = parseInt(t[1]);
let day = t[2];
if (month === 1 && (day === "29"||day === "30"||day === "31") ) {
if(!this.isLeapYear(year)){
day="28";
}else{
day="29";
}
}
if((month==3||month==5||month==8||month==10)&&day === "31"){
day="30";
}
if(month==12){
year = year+1;
month=1;
}else{
month=month+1;
}
let m = (month>9?(""+month):"0"+month);

return year+"-"+m+"-"+day;
},
getNextYearDate(d) {

let t = d.split("-");
console.log(t)
let year = parseInt(t[0])+1;
let month = parseInt(t[1]);
let day = t[2];
if (month === 2 && day === "29" ) {
if(!this.isLeapYear(year)){
day="28";
}else{
day="29";
}
}

let m = (month>9?(""+month):"0"+month);

return year+"-"+m+"-"+day;
},
isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
},

posted @ 2025-09-30 16:25  雪儿蛇王  阅读(64)  评论(0)    收藏  举报