/** * @author:mmm * @function: 获取输入日期的几个月前的日期 * @param date 输入日期(new Date()) * @param monthNum 月数 */ function preMonthDay(date, monthNum) { var currYear = new Date(date).getFullYear(); //获取当前日期的年份 var currMonth = new Date(date).getMonth(); //获取当前日期的月份 var currDay = new Date(date).getDate(); //获取当前日期的日 var currDays = new Date(currYear, currMonth, 0).getDate(); //获取当前日期中月的天数 var year2 = currYear; var month2 = parseInt(currMonth) - monthNum; if (month2 <= 0) { var absM = Math.abs(month2); year2 = parseInt(year2) - Math.ceil(absM / 12 == 0 ? 1 : parseInt(absM) / 12); month2 = 12 - (absM % 12); } var day2 = currDay; var days2 = new Date(year2, month2, 0).getDate(); //获取之前日期中月的天数 if (day2 > days2) { day2 = days2; } if (month2 < 10) { month2 = "0" + month2; } return new Date(year2, month2, day2); };