Date object to get the time

Methods for creating Date objects

const d = new Date()
console.log(d)//Wed May 11 2022 21:52:10 GMT+0800 (中国标准时间)

1. Convert date format

console.log(d.toLocaleString())//2022/5/11 21:53:30
console.log(d.toLocaleDateString())//2022/5/11
console.log(d.toLocaleTimeString())//21:55:57

2. Get the year, month, day, hour, minute, and second

console.log(d.getFullYear())//2022
//月份范围 0-11下标 对应1-12月
console.log(d.getMonth())//4
console.log(d.getDate())//11
//星期范围 0-6 对应周日到周六
console.log(d.getDay())//3
console.log(d.getHours())//22
console.log(d.getMinutes())//0
console.log(d.getSeconds())//44

3. Get the timestamp: return the number of milliseconds from January 1, 1970, to this moment

The function of time stamp: solve the problem of time difference in different regions of the earth

console.log( + new Date())//1652277606718
console.log(Date.now())//1652277606718

Guess you like

Origin blog.csdn.net/Qiemo_/article/details/124720005