function addMonth(date, offset) {
if (date instanceof Date && !isNaN(offset)) {
let givenMonth = date.getMonth();
let newMonth = givenMonth + offset;
date.setMonth(newMonth);
return date;
}
}
1 参数 date是 var date = new Date();这种类型。offset是增加或减少的月份的数字。
2 获取日期的年月日
var year = date.getFullYear(); // 返回的是年份
var month = date.getMonth() + 1; // 返回的月份上个月的月份,记得+1才是当月
var dates = date.getDate(); // 返回的是几号