JavaScript Date.now() method
Example
Return the number of milliseconds for the current time:
const
currentTime
=
Date
.
now
(
)
;
console.
log
(
currentTime
)
;
// Output similar to: 1633072800000
Try it »
In this example,currentTimeThe variable will hold the number of milliseconds of the current time. Each time this code runs, the output value will be different, because it always returns the current time.
Definition and Usage
Date.now() Is Date A static method of the object that returns the number of milliseconds of the current time.
Date.now() This method is very useful, especially when you need to measure time intervals or generate unique timestamps.
Date.now() The method returns the number of milliseconds since January 1, 1970 00:00:00 UTC (Coordinated Universal Time); this point in time is known as the start of the "Unix epoch" or "Unix timestamp".
Syntax
Date.now()
Return Value
Date.now() Returns a number representing the milliseconds between the current time and the Unix epoch.
Browser support





All major browsers support the getDate() method.
Date.now() The difference between new Date().getTime() and
You may already know that,new Date().getTime()can also be used to get the number of milliseconds of the current time. So,Date.now()andnew Date().getTime()what is the difference between them?
Difference
-
Performance:
Date.now()compared tonew Date().getTime()is more efficient. BecauseDate.now()directly returns the number of milliseconds of the current time, whilenew Date().getTime()needs to first create aDateobject, and then callgetTime()method. -
Code conciseness:
Date.now()code is more concise, because it does not need to create aDateobject.
Example
// Using Date.now()
const
time1
=
Date
.
now
(
)
;
// Using new Date().getTime()
const
time2
=
new
Date
(
)
.
getTime
(
)
;
console.
log
(
time1
===
time2
)
;
// Output: true
Although both produce the same result, in scenarios with high performance requirements it is recommended to useDate.now().
Date.now() Use Cases for the
Date.now()The method has a variety of application scenarios in real development; here are some common examples:
Measuring code execution time
You can use theDate.now()to measure the execution time of a piece of code.
Example
const
startTime
=
Date
.
now
(
)
;
// Simulate some time-consuming operations
for
(
let i
=
0
;
i
<
1000000
;
i
++
)
{
// Some calculations
}
const
endTime
=
Date
.
now
(
)
;
console.
log
(
`Code execution time: $
{
endTime
startTime
}
ms`
)
;
In this example,endTime - startTimewill give the time spent executing the code (in milliseconds).
Generating a unique timestamp
Date.now()can be used to generate a unique timestamp, typically used to identify an event or an operation.
Example
const
uniqueTimestamp
=
Date
.
now
(
)
;
console.
log
(
uniqueTimestamp
)
;
// Output the number of milliseconds of the current time
BecauseDate.now()The returned value is unique (at least unique at the millisecond level), so it can be used to generate a unique identifier.
Cache-Control
In Web development,Date.now() can be used to generate a cache-control timestamp to ensure the browser does not use stale cache.
Example
const
cacheBuster
=
Date
.
now
(
)
;
const
url
=
`https
:
//example.com/data.json?cache=${cacheBuster}`;
In this example, each request carries a different timestamp, thereby preventing the browser from using stale cached data.
Notes
AlthoughDate.now()is very useful, but the following points should be noted when using it:
-
Precision:
Date.now()The returned time precision is at the millisecond level. If higher precision (such as microsecond level) is needed, you can useperformance.now(). -
Timezone:
Date.now()The returned time is based on UTC and is not affected by the local time zone. If you need to handle local time, you can usenew Date(). -
Compatibility:
Date.now()is supported in most modern browsers, but may not be supported in some very old browsers. If you need to be compatible with these browsers, you can usenew Date().getTime()as an alternative.
JavaScript Date object