Collection of usage of front-end Date objects

Date objects are used to handle dates and times.

create object

var d = new Date();
var d = new Date(milliseconds); // 参数为毫秒
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

method

getDate()

Get the current date object (1-31)

const d = new Date()
    console.log('d.fetDate', d.getDate())

getDay()

Get the day of the week of the current date object (return0-6.0表示周日.1表示周一)

const d = new Date()
    console.log('d.getDay', d.getDay())

 getMonth()

Payment before month (0-11.0表示一月.11表示12月)

const d = new Date()
    console.log('d.getManth', d.getMonth())

 geFullYear()

Get the current year

 const d = new Date()
    console.log('d.getFullYear', d.getFullYear())

 hours()

Taking time(0-23)

const d = new Date()
    console.log('d.getHours', d.getHours())

getMinutes()

Take-to-share (0-59

const d = new Date()
    console.log('d.getMinutes', d.getMinutes())

geSeconds()

Seconds taken(0-59)

const d = new Date()
    console.log('d.getSeconds', d.getSeconds())

 geMilliseconds()

Get milliseconds

const d = new Date()
    console.log('d.getMilliseconds', d.getMilliseconds())

 Three ways to get timestamp

const d = new Date()
    console.log('获取时间戳', d.getTime(), d.valueOf(), Date.now())

Get Time

toLocaleTimeString()

const d = new Date()
    console.log('获取时间', d.toLocaleTimeString()) // 20:31;58

toTimeString()

const d = new Date()
    console.log('获取时间', d.toTimeString()) // 10:42:56 GMT+0800 (GMT+08:00)

Get date

toLocaleDateString()

const d = new Date()
    console.log('获取日期', d.toLocaleDateString()) // 2023/11/23

toDateString()

 const d = new Date()
    console.log('获取日期', d.toDateString()) // Thu Nov 23 2023

Use of related modules

moment.js

JS common time operations moment.js reference document-CSDN Blog

Comparison between day.js and moment.js_Malu Laomo 007’s Blog-CSDN Blog

Some related scenes of time

Have you ever encountered the clearing pit of el-date-picker_Malu Laomo 007’s Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/2201_75705263/article/details/134570787