js 计算前一天或几天的日期,以及后一天或几天的日期
代码
function getDay(day) {
var today = new Date();
var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
today.setTime(targetday_milliseconds); //注意,这行是关键代码
var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = doHandleMonth(tMonth + 1);
tDate = doHandleMonth(tDate);
return tYear + "-" + tMonth + "-" + tDate;
}
function doHandleMonth(month) {
var m = month;
if (month.toString().length == 1) {
m = "0" + month;
}
return m;
}
使用
后一天 getDay(1) 返回 2023-02-11
前一天 getDay(-1) 返回 2023-02-09
标签:tMonth,几天,tDate,month,日期,var,js,today
From: https://www.cnblogs.com/chunyouqudongwuyuan/p/17108613.html