// 增加年份
function addYears(date, years) {
date.setFullYear(date.getFullYear() + years);
return date;
}
// 减少年份
function subtractYears(date, years) {
date.setFullYear(date.getFullYear() - years);
return date;
}
// 增加月份
function addMonths(date, months) {
date.setMonth(date.getMonth() + months);
return date;
}
// 减少月份
function subtractMonths(date, months) {
date.setMonth(date.getMonth() - months);
return date;
}
// 增加天数
function addDays(date, days) {
date.setDate(date.getDate() + days);
return date;
}
// 减少天数
function subtractDays(date, days) {
date.setDate(date.getDate() - days);
return date;
}
// 示例
var currentDate = new Date();
console.log("Current date:", currentDate);
currentDate = addYears(currentDate, 1);
console.log("Date after adding 1 year:", currentDate);
currentDate = subtractMonths(currentDate, 6);
console.log("Date after subtracting 6 months:", currentDate);
currentDate = addDays(currentDate, 15);
console.log("Date after adding 15 days:", currentDate);
09-17
450
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-26
117
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-30
12-12
460
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)