function getTime(days,flag){
let current = +new Date()
let difference = days*24*3600*1000;
let result = 0;
if(flag === 'front'){
result = current - difference;
}else if(flag === 'after'){
result = current + difference;
}else{
throw Error('缺少flag参数');
}
const data = new Date(result);
let Y = data.getFullYear(),
M = data.getMonth()+1 >9 ? data.getMonth()+1: '0'+ (data.getMonth()+1),
D = data.getDate()>9 ? data.getDate(): '0'+ data.getDate(),
h = data.getHours()>9? data.getHours(): '0' + data.getHours(),
m = data.getMinutes()>9? data.getMinutes(): '0' + data.getMinutes(),
s = data.getSeconds()>9? data.getSeconds(): '0'+ data.getSeconds();
const time = `${Y}-${M}-${D} ${h}:${m}:${s}`
return time
}
console.log("getTime(3)",getTime(5,'after'))
网友评论