JS获取当前时间的昨天以及任意一天
function getNowFormatDate(day?) {
const date = new Date()
if (day) {
date.setTime(date.getTime() + 24 * day * 60 * 60 * 1000)
}
const seperator1 = '-'
const year: any = date.getFullYear()
let month: any = date.getMonth() + 1
let strDate: any = date.getDate()
if (month >= 1 && month <= 9) {
month = '0' + month
}
if (strDate >= 0 && strDate <= 9) {
strDate = '0' + strDate
}
const currentdate = year + seperator1 + month + seperator1 + strDate
return currentdate
}
console.log(getNowFormatDate(), '今天')
console.log(getNowFormatDate(-1), '昨天')
console.log(getNowFormatDate(1), '明天')
console.log(getNowFormatDate(-7), '7天前')
console.log(getNowFormatDate(3), '3天后')