JavaScript Date.now() Method

Date 对象参考手册JavaScript Date Object

Example

Returns the milliseconds of the current time:

const currentTime = Date.now();
console.log(currentTime); // Output similar to: 1633072800000

Try it Yourself »

In this example,currentTimethe variable will store the milliseconds of the current time. Every time this code runs, the output value will be different, because it always returns the current time.


Definition and Usage

Date.now()YesDateA static method of the object, which returns the current time in milliseconds.

Date.now()The 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), a point in time known as the start of the "Unix Epoch" or "Unix Timestamp".

Syntax

Date.now()

Return Value

Date.now()Returns a number representing the number of milliseconds between the current time and the Unix Epoch.

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the getDate() method


Date.now()andnew Date().getTime()The Difference

You may already know that,new Date().getTime()it can also be used to get the milliseconds of the current time. So,Date.now()andnew Date().getTime()what is the difference between them?

Difference

  1. Performance:Date.now()Comparenew Date().getTime()is more efficient. BecauseDate.now()it directly returns the milliseconds of the current time, whilenew Date().getTime()it needs to first create aDateobject, and then call thegetTime()method.

  2. Code Simplicity:Date.now()The 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 the results of both are the same, in scenarios with high performance requirements, it is recommended to useDate.now()。


Date.now()Application Scenarios

Date.now()The method has multiple application scenarios in actual development. Here are some common examples:

Measuring Code Execution Time

You can useDate.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}milliseconds`);

In this example,endTime - startTimewill give the time spent executing the code (in milliseconds).

Generating Unique Timestamps

Date.now()can be used to generate unique timestamps, usually to identify a certain event or operation.

Example

const uniqueTimestamp = Date.now();
console.log(uniqueTimestamp); // Output the milliseconds of the current time

SinceDate.now()the value returned is unique (at least unique at the millisecond level), it can be used to generate unique identifiers.

Cache Control

In web development,Date.now()can be used to generate cache control timestamps to ensure the browser does not use expired caches.

Example

const cacheBuster = Date.now();
const url = `https://example.com/data.json?cache=${cacheBuster}`;

In this example, each request is accompanied by a different timestamp, thus preventing the browser from using old cached data.


Notes

AlthoughDate.now()it is very useful, you need to note the following points when using it:

  1. Precision:Date.now()The time precision returned is at the millisecond level. If you need higher precision (such as microsecond level), you can useperformance.now()。

  2. Time Zone:Date.now()The time returned is based on UTC and is not affected by the local time zone. If you need to handle local time, you can usenew Date()。

  3. Compatibility:Date.now()It 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.


Date 对象参考手册JavaScript Date Object

Other Extensions