function GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
var y = dd.getFullYear();
var m = dd.getMonth()+1;//获取当前月份的日期
var d = dd.getDate();
return y+"-"+m+"-"+d;
}
document.write("前天:"+GetDateStr(-2));
document.write("
昨天:"+GetDateStr(-1));
document.write("
今天:"+GetDateStr(0));
document.write("
明天:"+GetDateStr(1));
document.write("
后天:"+GetDateStr(2));
document.write("
大后天:"+GetDateStr(3));
结果 2022-1-1
https://zhuanlan.zhihu.com/p/23835062
补零的方法
结果 2022-01-01
GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
var y = dd.getFullYear();
var m = dd.getMonth()+1;//获取当前月份的日期
var d = dd.getDate();
return `${y}-${this.compareNumber(m)}-${this.compareNumber(d)}`
},
compareNumber (num) {
return num < 10 ? '0' + num : num
},
https://blog.csdn.net/m0_56272227/article/details/127803556
标签:dd,js,write,获取,GetDateStr,var,AddDayCount,document,昨日 From: https://www.cnblogs.com/kaibindirver/p/17061758.html