Date and time are compulsory courses that cannot be bypassed in the programming process. Fortunately JavaScript
has prepared a built-in object Date (date) for us. Through this object, we can create, store, modify, measure time, print and other basic operations. It is one of the required courses for every JavaScript
programmer.
is the same as ordinary objects. We can use new Date()
to create a Date
object, and we can also pass in some initialization parameters during creation.
without parameters:
let now = new Date()console.log(now)
The code execution results are as follows:
This simply outputs the specific time when the code is executed. What is shown here is 2022年6月22日19点25份24秒
.
creates a date object with millisecond parameters, where milliseconds
refers to the number of milliseconds since UTC+0 on January 1, 1970 (1 millisecond = 1/1000 seconds).
//Create a time, for example 1970.1.1 time point 0 milliseconds let jan01_1970 = new Date(0) console.log(jan01_1970) //Create a time that is 3 days later than the 1.1 time point in 1970 (3 days * 24 hours * 3600 seconds * 1000 milliseconds) let jan04_1970 = new Date(3 * 24 * 3600 * 1000) Console.log(jan04_1970)
code execution results:
milliseconds
is the number of milliseconds that have passed since 00:00:00 on January 1, 1970, or it is called a timestamp .
Timestamp is a simple digital representation of date. We usually use new Date(milliseconds)
to create a date. If we already have a Date
object, we can use date.getTime()
to get the timestamp corresponding to the date.
Note:
China is in Zone 8, so the time in the above example is not 00:00:00, but 08:00:00,
The timestamp is not only an integer, it can also be a negative number, for example:
//1969-12-31 00:00:00let dec31_1969 = new Date(-24 * 3600 * 1000)console.log(dec31_1969)
code execution result:
Ifuses a timestamp every time it is created, it may not be convenient, because timestamp calculation is still a bit difficult.
We can also use a time string to create a time, for example:
let date = new Date('2022-06-22 00:00:00') console.log(date)
code execution results:
year
——must be a four-digit number;month
—— [0,11]
, 0
means January;1
;hours/minutes/sec/ms
- the default is 0;for example:
let date = new Date(2022,6,22,20,35,33)console.log(date)
code execution results:
We can also specify the number of milliseconds:
let date = new Date(2022,6,22,20,35,33,777)console.log(date)
If we have a Date
object, we can pass the built-in method of Date
object , get a part of time, such as year, month, date, etc.
For example, we have date
:
let date = new Date(2022,5,22,20,35,33)
getFullYear()
gets the year, date.getFullYear()
returns 2022
;getMonth()
gets the month, date.getMonth()
returns 5
, which is 6
;getDate()
gets the date of the current month, date.getDate()
returns 22
;getDay()
gets the day of the week the current time is on, date.getDay()
returns 3
;Note:
- The above dates They are all based on local dates. For example, I am in the time zone of China.
- To get the year, you must use
getFullYear
.getYear
will return the two-digit year.
We can also get the time in the 0
time zone, which is UTC
time, corresponding to getUTCFullYear()
and getUTCMonth()
, getUTCDay
, etc. Just insert UTC
after get
.
For example:
let date = new Date(2022,5,22,20,35,33)console.log(date.getHours(),date.getUTCHours())
code execution results:
It can also be seen at night that the difference between China time zone and 0
time zone is 8
hours.
getTime()
returns the timestamp of the date, and the method does not have a UTC
mode;getTimezoneOffset()
returns the time difference between the local time zone and the 0
time zone, in minutes, and there is no UTC
mode;we can also set the date through the method of Date
object A certain part of:
setFullYear(year, [month], [date])
sets the year (month, day)setMonth(month, [date])
sets the month (day)setDate(date)
sets the date (the day of the month)setHours(hour, [min], [sec], [ms])
Set hours (minutes, seconds, milliseconds)setMinutes(min, [sec], [ms])
Set minutes (seconds, milliseconds)setSeconds(sec, [ms])
Set seconds (milliseconds)setMilliseconds(ms)
Set millisecondssetTime(milliseconds)
(use the number of milliseconds since 1970-01-01 00:00:00 UTC+0
to set the entire date)Among the above functions, only setTime()
does not UTC
variant.
JavaScript
's Date
has an automatic calibration function, which provides us with great convenience in calculating time.
For example:
let date = new Date(2022,5,38)//Note the execution results of the 38console.log(date) code here
:
From the execution results, we can see that "June 38, 2022" did not cause an error in the program, but converted the date to "July 8, 2022".
The above case has verified that in JavaScript
, dates outside the range will be automatically assigned by the Date
object. In this way, we can use Date
object to calculate dates very conveniently.
For example, we can add and subtract the year, month, and day on the date:
let date = new Date(2022,5,23)//The current time is 2022-6-23date.setDate(date.getDate() + 8)//Calculate the time after eight days console.log(date)
code execution results:
Similarly, we can also use the date.setSeconds()
method to calculate the date in seconds.
and date Date
is the same as the return value of date.getTime()
, which is a value in milliseconds:
let date = new Date()console.log(+date)
code Execution result:
Since the essence of time is a number, we can also perform time difference calculations in ms
(milliseconds).
For example:
let date1 = new Date(2022,5,23)let date2 = new Date(2022,5,24)console.log(`The time difference is ${date2-date1}ms`)
The code execution result is:
If we want to get the current time, a better approach is to use the Date.now()
method. This method will return the timestamp of the current time without creating additional Date
objects, which is good for memory overhead and garbage collection, and the code is simpler.
For example:
let begin = Date.now()for(let i = 1;i<100000;i++){ let num = i * i * i;}let end = Date.now()console.log(`consume time ${end - begin}ms`)
code execution results:
The Date.parse()
method can read a date string and convert it into a timestamp, but the string must comply with certain rules: YYYY-MM-DDTHH:mm:ss.sssZ
.
YYYY-MM-DD
corresponds to年-月-日
T
belongs to the delimiterHH:mm:ss.sss
corresponds to时:分:秒.毫秒
Z
can be a time zone in the +-hh:mm
format. A single character Z
represents a UTC+0
string that can be written in abbreviated ways, for example: YYYY-MM-DD
, YYYY-MM
, YYYY
.
To give you a little tip:
let ms = Date.parse('2022-06-23T19:38:30.777+08:00')//Time stamp let date = new Date(ms)console.log(date)
code execution result:
JavaScript
uses Date
object to process time: new Date()
;Date
has many practical methods, and we can get a certain period of time;Date
object will be automatically calibrated, and we can directly add and subtract dates;Date.now()
can efficiently obtain the current time;