Date.now()
Return the current time as milliseconds elapsed since the Unix epoch.
Date.now()- Added in JavaScript ES5
Return value
numberA number: the current time as milliseconds since the epoch, midnight at the beginning of 1 January 1970, UTC.
Changelog
- PHP —Browsers reduce the resolution of the returned value as a fingerprinting defence. Firefox rounds to 2 ms by default, and to 100 ms when privacy.resistFingerprinting is enabled, so sub-millisecond timings are not observable.
A static method on Date, not on an instance. It gives you the same number as new Date().getTime() without allocating an object, which is why it is the standard choice inside loops and hot paths.
When to reach for it
Use it for anything that is arithmetic on time rather than a calendar operation: measuring how long an operation took, stamping a record, computing a cache expiry, throttling, or comparing two moments. Since the value is a plain number, differences and comparisons work with ordinary operators.
What trips people up
It is not monotonic. The value comes from the system clock, which can be adjusted by the user or by NTP. Two consecutive calls can therefore go backwards, and an elapsed-time measurement can come out negative or wildly wrong. For measurement that must be reliable, performance.now() is the monotonic alternative — though it is a browser API, not part of the language.
The unit is milliseconds, not seconds. Most backend systems, JWT exp claims and Unix time() deal in seconds. Mixing the two silently produces dates in 1970 or in the year 50000. Divide by 1000 and floor when crossing that boundary.
Resolution is also deliberately coarsened by browsers, so do not expect to time anything shorter than a couple of milliseconds.
Example
When to use it
- Time how long a computation or a request took, to the nearest millisecond
- Stamp created_at / updated_at fields without allocating a Date object
- Check whether a cached value is older than a time-to-live
- Build a deadline by adding a duration to the current moment
Frequently asked questions
What is the difference between Date.now() and performance.now()?
Date.now() returns wall-clock milliseconds since 1970 and can jump if the system clock is corrected, while performance.now() returns a monotonic, fractional-millisecond value since page load that never goes backwards. Use Date.now() for timestamps and performance.now() for benchmarking.
How do I get a Unix timestamp in seconds in JavaScript?
Use Math.floor(Date.now() / 1000), since Date.now() returns milliseconds. Flooring rather than rounding matters, because rounding can push you a second into the future.
Why does Date.now() return the same value on consecutive calls?
Because the calls completed within the clock's resolution, and browsers deliberately coarsen that resolution — Firefox to 2 ms, or 100 ms with resistFingerprinting on — as an anti-fingerprinting measure.
More examples
Milliseconds vs. seconds
A 13-digit number is milliseconds, a 10-digit one is seconds. The last two lines show the classic bug: passing a seconds timestamp straight to new Date() puts you a few days after the epoch.
const ms = Date.now();
const seconds = Math.floor(ms / 1000);
console.log(String(ms).length); // 13 digits
console.log(String(seconds).length); // 10 digits
// Feeding a seconds value to the Date constructor lands in 1970
console.log(new Date(seconds).getUTCFullYear() < 1990);
console.log(new Date(seconds * 1000).getUTCFullYear() >= 2024);A time-to-live check
Storing an absolute expiry (now plus the TTL) means each read is one comparison of two numbers. The fresh entry comes back; the unknown key returns undefined.
const cache = new Map();
const TTL = 60_000; // one minute
function put(key, value) {
cache.set(key, { value, expires: Date.now() + TTL });
}
function get(key) {
const entry = cache.get(key);
if (!entry || Date.now() > entry.expires) return undefined;
return entry.value;
}
put("user:1", { name: "Ada" });
console.log(get("user:1"));
console.log(get("missing"));Comparing against an instance method
getTime(), Number() coercion and the unary plus all yield the same timestamp as Date.now(), but each allocates a Date first. Date.now() is the one to use when you only want the number.
const a = Date.now();
const b = new Date().getTime();
const c = Number(new Date());
const d = +new Date();
// All four produce the same kind of value
console.log([a, b, c, d].every((n) => typeof n === "number"));
console.log(Math.abs(b - a) < 1000);
console.log(Math.abs(d - c) < 1000);
Discussion