Date.now
Returns the current time as a number of milliseconds.
Available since: ES5
Syntax
Date.now()Return value
numberThe number of milliseconds since January 1, 1970 UTC.
Examples
The current timestamp:
console.log(Date.now());Output:
1791014400000How long an operation took:
const start = Date.now();
let sum = 0;
for (let i = 0; i < 1e6; i++) sum += i;
console.log(`Took ${Date.now() - start} ms`);Output:
Took 4 msNotes
The number is different on every run. Such a timestamp is easy to store and compare: a bigger number is a later moment. It equals new Date().getTime() without creating an object.
See also
- Date — A date and time: creating, reading parts, calculating.
- Date.prototype.getTime — Returns the date as a number of milliseconds.
- console.time — Measures how long code took to run.
Related recipes
In the tutorial
- Dates and time — The Date object in JavaScript: creating a date, reading the year, month and day, changing a date, the difference between dates and the ISO format.