/** * 计算给定日期 n个月后的日期 * @param dateStr * @param num * @returns {string} */ function addMonth(dateStr, num) { var monthNum = 0; if (typeof num == "string") { monthNum = parseInt(num); } else { monthNum = num; } var date = new Date(dateStr); //获取原日 var day = date.getDate(); //获取原月份 var month = date.getMonth(); //设置增加月份 date.setMonth(date.getMonth() + monthNum * 1, 1); //获取增加的后的月份 var Jmonth = date.getMonth() + 1; //获取增加的后的年份 var Jyear = date.getFullYear(); if (Jmonth == 4 || Jmonth == 6 || Jmonth == 9 || Jmonth == 11) { //小月 if (day > 30) { day = 30; } } else if (Jmonth == 2) { //2月判断是否闰年 if ((Jyear % 4 == 0 && Jyear % 100 != 0) || Jyear % 400 == 0) { if (day > 29) { day = 29; } } if (day > 28) { day = 28; } } else { //大月 if (day > 31) { day = 31; } } Jmonth = Jmonth < 10 ? "0" + Jmonth : Jmonth; day = day < 10 ? "0" + day : day; return Jyear + "-" + Jmonth + "-" + day; }标签:Jyear,指定,JS,var,日期,num,date,day,Jmonth From: https://www.cnblogs.com/Tom2-xiao-ye/p/17732397.html