Jsref Now
Title: JavaScript Date now() Method | Rookie Tutorial
[ JavaScript Date Object](#)
## Example
Return the number of milliseconds for the current time:
const currentTime =Date.now();
console.log(currentTime);// Output similar to: 1633072800000
[Try it out Β»](#)
In this example, the `currentTime` variable will hold the number of milliseconds for the current time. Each time this code is run, the output value will be different because it always returns the current time.
* * *
## Definition and Usage
`Date.now()` is a static method of the `Date` object that returns the number of milliseconds for the current time.
The `Date.now()` method is very useful, especially when measuring time intervals or generating unique timestamps.
The `Date.now()` 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 "Unix Epoch" or the starting point of the "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

All major browsers support the getDate() method
* * *
## Difference between `Date.now()` and `new Date().getTime()`
You might already know that `new Date().getTime()` can also be used to get the number of milliseconds for the current time. So, what is the difference between `Date.now()` and `new Date().getTime()`?
### Differences
1. **Performance**: `Date.now()` is more efficient than `new Date().getTime()`. This is because `Date.now()` directly returns the number of milliseconds for the current time, while `new Date().getTime()` first needs to create a `Date` object and then call the `getTime()` method.
2. **Code Conciseness**: The code for `Date.now()` is more concise because it does not require creating a `Date` object.
## 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 are the same, it is recommended to use `Date.now()` in scenarios with high performance requirements.
* * *
## Application Scenarios of `Date.now()`
The `Date.now()` method has various application scenarios in actual development. Here are some common examples:
### Measuring Code Execution Time
You can use `Date.now()` to measure the execution time of a piece of code.
## Example
const startTime =Date.now();
// Simulate some time-consuming operation
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 - startTime` will give the time taken (in milliseconds) for the code to execute.
### Generating Unique Timestamps
`Date.now()` can be used to generate unique timestamps, often used to identify an event or operation.
## Example
const uniqueTimestamp =Date.now();
console.log(uniqueTimestamp);// Outputs the number of milliseconds for the current time
Since the value returned by `Date.now()` is unique (at least at the millisecond level), it can be used to generate unique identifiers.
### Cache Control
In web development, `Date.now()` can be used to generate timestamps for cache control to ensure the browser does not use outdated cache.
## Example
const cacheBuster =Date.now();
const url = `https://example.com/data.json?cache=${cacheBuster}`;
In this example, each request will include a different timestamp, thus preventing the browser from using cached old data.
* * *
## Notes
Although `Date.now()` is very useful, the following points should be noted when using it:
1. **Precision**: The time precision returned by `Date.now()` is at the millisecond level. If higher precision is needed (such as microsecond level), `performance.now()` can be used.
2. **Time Zone**: The time returned by `Date.now()` is based on UTC and is not affected by the local time zone. If you need to handle local time, you can use `new Date()`.
3. **Compatibility**: `Date.now()` is supported in most modern browsers, but it might not be supported in some very old browsers. If you need compatibility with these browsers, you can use `new Date().getTime()` as an alternative.
* * JavaScript Date Object](#)
YouTip