日期时间戳的使用与计算

new Date()的使用

const date = new Date()
console.log(date) // 2021-09-10T07:12:11.948Z
console.log(typeof date) // object
console.log(date * 1) //1631257931948

Date()的使用

const hd = Date()
console.log(hd) // Fri Sep 10 2021 15:12:11 GMT+0800 (中国标准时间)
console.log(typeof hd) // string
console.log(hd * 1) // NaN

Date.now()

const date1 = Date.now()
console.log(date1) // 1631257931955

ISO与TimeStamp格式转换

const d = new Date('1996-7-12 08:22:12')
console.log(d * 1) // 1631258155748
console.log(Number(d)) // 837130932000
console.log(d.valueOf()) // 837130932000
console.log(d.getTime()) // 837130932000

const timestamp = d.valueOf()
console.log(new Date(timestamp)) // 1996-07-12T00:22:12.000Z

Date()下可用的API

const d = new Date('1996-7-12 08:22:12')

console.log(d) // 1996-07-12T00:22:12.000Z
console.log(d.getFullYear()) // 1996
console.log(d.getMonth() + 1) // 7
console.log(d.getDate()) // 12
console.log(d.getHours()) // 8
console.log(d.getMinutes()) //22
console.log(d.getSeconds()) //12