JavaScript Reference ยท Date and Intl
Date.now()
Reference for JavaScript Date.now(): get the current time as milliseconds since the Unix epoch, measure elapsed time, and know when to use performance.now().
Date.now() is a static method that returns the current time as a number: the milliseconds that have passed since midnight UTC on January 1, 1970 (the Unix epoch). It is the cheapest way to get a timestamp because it does not create a Date object.
Use it for timestamps in logs, cache expiry checks, and rough timing. For precise measurement of short durations, performance.now() is the better tool.
const started = Date.now(); // ... do some work ... const elapsed = Date.now() - started;
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Date.now()
Parameters: none.
Return value: an integer number of milliseconds since the Unix epoch, based on the system clock.
Exceptions: none.
Usage notes
- It is a static method. Call it on
Date, not on a date object:Date.now(), neversomeDate.now(). - UTC based. The number is the same everywhere in the world at the same instant. Time zones only matter when you format it.
- The system clock can change. If the user or the network adjusts the clock,
Date.now()can jump forward or even backward. Do not rely on it for measuring short durations; useperformance.now(), which only moves forward. - Reduced precision. To make timing attacks harder, browsers may round timestamps. Do not expect exact millisecond accuracy.
- Equivalent forms:
new Date().getTime()and+new Date()give the same number but create a throwaway object. PreferDate.now(). - Hard to test. Code that calls
Date.now()directly gives different results every run. Pass the time in as a parameter, or replace the function in tests, so your logic stays predictable. - Seconds versus milliseconds. Many servers and APIs use seconds. Divide by 1000 and round (
Math.floor(Date.now() / 1000)) when you need that.
Browser support
Date.now() has been available in every browser and runtime for many years.