第一种
比较笨的方法
const now = new Date()
const year = now.getFullYear()
const month = now.getMonth() + 1
const day = now.getDate()
const LastWeekDate = new Date(now.getTime() - 7 * 24 * 3600 * 1000)
const lastYear = LastWeekDate.getFullYear()
const lastMonth = LastWeekDate.getMonth() + 1
const LastDay = LastWeekDate.getDate()
//现在的时间
let clock = `${year}-`
if (month < 10) clock += "0"
clock += `${month}-`
if (day < 10) clock += "0"
clock += `${day}`
//一周前的时间
let lastClock = `${lastYear}-`
if (lastMonth < 10) lastClock += "0"
lastClock += `${lastMonth}-`
if (LastDay < 10) lastClock += "0"
lastClock += `${LastDay}`
第二种
用到了moment.js插件
const now = new Date()
const year = now.getFullYear()
const month = now.getMonth() + 1
const day = now.getDate()
const hour = now.getHours()
const minute = now.getMinutes()
const second = now.getSeconds()
let clock = `${year}-`
if (month < 10) clock += "0"
clock += `${month}-`
if (day < 10) clock += "0"
clock += `${day} `
if (hour >= 0 && hour <= 9) clock += "0"
clock += `${hour}:`
if (minute >= 0 && minute <= 9) clock += "0"
clock += `${minute}:`
if (second >= 0 && second <= 9) clock += "0"
clock += `${second}`
//现在的时间
moment(clock).format("YYYY-MM-DD HH:mm:ss"),
//一周前的时间
moment(clock).add(-7, "d").format("YYYY-MM-DD HH:mm:ss")
效果: