monthDay(year, month) { month = parseInt(month, 10); let d = new Date(year, month, 0); // 这个是都可以兼容的 let date = new Date(year + "/" + month + "/0"); // IE浏览器可以获取天数,谷歌浏览器会返回NaN return d.getDate(); } getNextMonth(date, length) { let yy = date.getFullYear(); let mm = date.getMonth(); let dd = date.getDate(); let nm = 0; // 目标月份 nm = mm + length; let nd = 0; // 目标天数 if (this.monthDay(yy, nm + 1) < dd) { nd = this.monthDay(yy, nm + 1); } else { nd = dd - 1; } date.setDate(1); date.setMonth(nm); date.setDate(nd); return date; }
this.getNextMonth(new Date(), 2)