1. 日期格式转换
function getNowFormatDate(date) {
let seperator1 = "-";
let year = date.getFullYear();
let month = date.getMonth() + 1;
let strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
let currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
// 当日 日期
let date = new Date();
let today = getNowFormatDate(date);
2.获取n天前的日期 (调用就行了)
// 一周前的日期
let day1 = new Date();
day1.setTime(day1.getTime() - 7 * 24 * 60 * 60 * 1000);
let beforeWeekDay = getNowFormatDate(day1);
console.log( beforeWeekDay);