获取时间戳
const date = new Date().getTime()
通过时间戳来控制时间段
- 先获取当日零点的时间戳。
const time = new Date()
const Y = time.getFullYear().toString()
const M = (time.getMonth() + 1).toString().padStart(2, '0')
const D = time.getDate().toString().padStart(2, '0')
const time_stamp = + new Date(`${Y}-${M}-${D}`)
console.log(time_stamp)
zeroTimeStamp = time_stamp - 60 * 60 * 8 * 1000
console.log(zeroTimeStamp)
- 获取三天前的时间戳。
const day = 60 * 60 * 24 * 1000
const threeDays = zeroTimeStamp - (day * 3)
- 再使用下面的时间戳格式化日期
时间戳--->字符串时间
const toTimestr = (time_stamp) => {
time_stamp = Number((time_stamp + '').padEnd(13, '0'))
const time = new Date(time_stamp)
const Y = time.getFullYear()
const M = (time.getMonth() + 1).toString().padStart(2, '0')
const D = time.getDate().toString().padStart(2, '0')
const h = time.getHours().toString().padStart(2, '0')
const m = time.getMinutes().toString().padStart(2, '0')
const s = time.getSeconds().toString().padStart(2, '0')
return `${Y}-${M}-${D} ${h}:${m}:${s}`
}
字符串时间--->时间戳
const res = + new Date('2022-02-12')