Summary of knowledge related to Date in Javascript

Table of contents

Get Time

Two ways to get the timestamp of the current time

Get year, month, day, hour, minute and second

Parameter description of Date() constructor

How to get time

Example

Pay attention to several confusing and error-prone methods

Time conversion

Description of T and Z in Date, and conversion issues between standard time and Beijing time


Get Time

Two ways to get the timestamp of the current time

1、 Date.now()

Gets the number of milliseconds from January 1, 1970, 0:00:00 (UTC, Coordinated Universal Time) to the current time

2、new Date().getTime()

Gets the number of milliseconds from 0:00:00 on January 1, 1970 (UTC, that is, Coordinated Universal Time) to the time represented by the Date object. (Earlier times will be represented by negative numbers)

         //获取当前时间-时间戳
         const currentTime1 = Date.now()
         console.log('currentTime1: ', currentTime1)
         const currentTime12 = new Date().getTime()
         console.log('currentTime12: ', currentTime12)

Get year, month, day, hour, minute and second

Parameter description of Date() constructor

First of all, you need to know that the Date constructor parameters have the following situations:

1、Date()

2. Date (time string)

3. Date (timestamp)

4. Date (year, month, day, hour, minute, second)

How to get time

1. Get the year getFullYear()

2. Get the month

3. Get the day of the month

4. Get the day of the week

5. When obtaining

6. Get points

7. Get seconds

Example

         //  Date()不传递参数
         const date0 = new Date()
         const year0 = date0.getFullYear()
         const month0 = date0.getMonth() + 1
         const day0 = date0.getDate()
         const week0 = date0.getDay()
         const hour0 = date0.getHours();
         const minute0 = date0.getMinutes();
         const second0 = date0.getSeconds();
         console.log('year: ', year0, ' month: ', month0, ' day: ', day0, ' week: ', week0, ' hour: ', hour0,
             ' minute: ',
             minute0, ' second: ', second0)

         // Date传递时间字符串--年月日的形式
         const date = new Date('2022-09-7 22:09:09')
         const year = date.getFullYear()
         const month = date.getMonth() + 1
         const day = date.getDate()
         const week = date.getDay()
         const hour = date.getHours();
         const minute = date.getMinutes();
         const second = date.getSeconds();
         console.log('year: ', year, ' month: ', month, ' day: ', day, ' week: ', week, ' hour: ', hour, ' minute: ',
             minute, ' second: ', second)

         // Date传递时间字符串--时间戳的形式
         const date2 = new Date(1662561716215)
         const year2 = date2.getFullYear()
         const month2 = date2.getMonth() + 1
         const day2 = date2.getDate()
         const week2 = date2.getDay()
         const hour2 = date2.getHours();
         const minute2 = date2.getMinutes();
         const second2 = date2.getSeconds();
         console.log('year: ', year2, ' month: ', month2, ' day: ', day2, ' week: ', week2, ' hour: ', hour2,
             ' minute: ', minute2, ' second: ', second2)

         // Date传递时间字符串--月日年时分秒的形式
         const date3 = new Date('September 7, 2022 11:45:12');
         const year3 = date3.getFullYear()
         const month3 = date3.getMonth() + 1
         const day3 = date3.getDate()
         const week3 = date3.getDay()
         const hour3 = date3.getHours();
         const minute3 = date3.getMinutes();
         const second3 = date3.getSeconds();
         console.log('year: ', year3, ' month: ', month3, ' day: ', day3, ' week: ', week3, ' hour: ', hour3,
             ' minute: ', minute3, ' second: ', second3)

         // Date传递年,月,日,时,分,秒的具体数值
         const date4 = new Date(2022, 8, 7, 22, 48, 13); //注意传递月份的时候需要实际月份减1
         const year4 = date4.getFullYear()
         const month4 = date4.getMonth() + 1
         const day4 = date4.getDate()
         const week4 = date4.getDay()
         const hour4 = date4.getHours();
         const minute4 = date4.getMinutes();
         const second4 = date4.getSeconds();
         console.log('year: ', year4, ' month: ', month4, ' day: ', day4, ' week: ', week4, ' hour: ', hour4,
             ' minute: ', minute4, ' second: ', second4)

Pay attention to several confusing and error-prone methods

1. The difference between getFullYear() and getYear()

getFullYear() obtains the complete year of the Date object (four-digit year)

getYear() obtains the relative time (relative to 1900)

2. The difference between getDate() and getDay()

getDate() gets the day of the month, its value range is 1~31

getDay() gets the day of the week, its value range is 0~6, 0 means Sunday

3. Why does getMonth() need to add 1?

getMonth() returns the month of the Date object. Its value range is 0~11. 0 represents the first month of the year, so we need getMonth()+1 when getting the actual month.

Time conversion

         // 时间戳转换为年月日时分秒
         const date = new Date(1662561716215)
         const year = date.getFullYear()
         const month = date.getMonth() + 1
         const day = date.getDate()
         const hour = date.getHours();
         const minute = date.getMinutes();
         const second = date.getSeconds();
         const time =
             `${year}-${dealDate(month)}-${dealDate(day)} ${dealDate(hour)}:${dealDate(minute)}:${dealDate(second)}`
         console.log('time: ', time)

         function dealDate(value) {
             return value < 10 ? '0' + value : value
         }

 

Description of T and Z in Date, and conversion issues between standard time and Beijing time

T stands for delimiter

Z represents time zone

The international standard time format is: 2022-09-07T23:00:00.000Z

Beijing time format: 2022-09-07 00:00:00

Because Beijing time is in the East Eighth District, adding 8 hours to the standard time is the East Eighth District time, which is Beijing time.

Therefore, when the standard time at or after 16:00 is converted to Beijing time, there will be an increase of 1

2022-09-07T15:59:59.000ZThe value obtained through getDate() is 7

2022-09-07T16:00:00.000ZThe value obtained through getDate() is 8

         const date = new Date('2022-09-07T15:59:59.000Z')
         const year = date.getFullYear()
         const month = date.getMonth() + 1
         const day = date.getDate()
         const week = date.getDay()
         const hour = date.getHours();
         const minute = date.getMinutes();
         const second = date.getSeconds();
         console.log('2022-09-07T15:59:59.000Z  year: ', year, ' month: ', month, ' day: ', day, ' week: ', week, ' hour: ', hour, ' minute: ',
             minute, ' second: ', second)

         const date2 = new Date('2022-09-07T16:00:00.000Z')
         const year2 = date2.getFullYear()
         const month2 = date2.getMonth() + 1
         const day2 = date2.getDate()
         const week2 = date2.getDay()
         const hour2 = date2.getHours();
         const minute2 = date2.getMinutes();
         const second2 = date2.getSeconds();
         console.log('2022-09-07T16:00:00.000Z  year: ', year2, ' month: ', month2, ' day: ', day2, ' week: ', week2, ' hour: ', hour2,
             ' minute: ', minute2, ' second: ', second2)

 

 

Guess you like

Origin blog.csdn.net/Celester_best/article/details/126754985