学习使用js给指定日期加减指定天数
具体代码
javascript
function add_day(date, days) {
if (days == undefined || days == '') {
days = 1;
}
let date_new = new Date(date);
date_new.setDate(date_new.getDate() + days);
let month = date_new.getMonth() + 1; //月份从0开始所以需要+1
var day = date_new.getDate();
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
return date_new.getFullYear() + '-' + month + '-' + day;
}
// 使用示例
var currentDate = '2024-04-30';
console.log("今天的日期:", currentDate);
var newDate = add_day(currentDate, 5);
console.log("加上5天后的日期:", newDate);