js获取一周前的日期

2,005 阅读1分钟
function getTragetTime(params) {
  let time = new Date().getTime() - 7 * 24 * 60 * 60 * 1000
  let tragetTime = new Date(time)  let month = tragetTime.getMonth()
  let day = tragetTime.getDate() 
 tragetTime = tragetTime.getFullYear() +'-' + (tragetTime.getMonth() > 9?tragetTime.getMonth() + 1 :'0'+ (tragetTime.getMonth() + 1)) +'-' + (tragetTime.getDate() > 9 ? tragetTime.getDate() : '0' + tragetTime.getDate())
  //console.log('这是一周前日期,格式为2021-01-01',tragetTime) 
 return tragetTime
}

let date=getTragetTime()