Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

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:

1791014400000

How 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 ms

Notes

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

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.

Thanks for reading!

Donate to the site — we work hard for you.