可以获取当前日期和自定义获取某天的日期。
function getFormatDate(customDate = 0) {
var nowDate = new Date();
var time = 24 * 60 * 60 * 1000;
var date = new Date(nowDate.getTime() + time * customDate) ;
var connect = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
if( month <= 9) {
month = "0" + month;
}
if(day <= 9) {
day = "0" + day;
}
var nowFormatDate = year + connect + month + connect + day;
return nowFormatDate;
}
//使用
var dateFomate1 = getFormatDate() ; //当前日期
var dateFomate2 = getFormatDate(-7) ; //7天前日期
var dateFomate3 = getFormatDate(30) ; //30天后日期