References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Date.now() method

Method JavaScript All modern browsers Updated
Quick answer

The Date.now() method returns the current time as a number — the milliseconds elapsed since January 1, 1970 (the Unix epoch). Date.now() might return 1782345600000. It's the quick way to get a timestamp or measure how long something takes, without creating a Date object.

Overview

Date.now() gives you the current moment as a single number: the count of milliseconds since the Unix epoch (midnight UTC on January 1, 1970). It's a static method on the Date constructor, so you call it directly without new — it's the shortcut for new Date().getTime().

Two uses dominate. First, timestamps: storing Date.now() records exactly when something happened, and the numbers are easy to store, compare and sort. Second, measuring elapsed time: capture Date.now() before and after an operation and subtract to get the duration in milliseconds — const start = Date.now(); doWork(); const ms = Date.now() - start;.

For high-precision performance timing, performance.now() is better — it offers sub-millisecond resolution and isn't affected by the system clock changing. But for everyday timestamps and rough timing, Date.now() is simpler and perfectly adequate. To turn a timestamp back into a readable date, pass it to the Date constructor: new Date(Date.now()).

Syntax

Date.now()  // e.g. 1782345600000 (ms since 1970)

// measure elapsed time
const start = Date.now();
// ...work...
const elapsed = Date.now() - start;  // milliseconds

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const start = Date.now();

  // do some busy work
  let sum = 0;
  for (let i = 0; i < 1e6; i++) sum += i;

  const elapsed = Date.now() - start;

  document.getElementById('out').textContent =
    'timestamp: ' + start + '\n' +
    'took: ' + elapsed + ' ms';
</script>

Best practices

  • Use Date.now() for timestamps and rough elapsed-time measurement.
  • It's simpler than new Date().getTime() — no object needed.
  • For high-precision timing, use performance.now() instead.
  • Convert a timestamp back to a date with new Date(timestamp).

Frequently asked questions

What does Date.now() return?
The current time as the number of milliseconds since January 1, 1970 (the Unix epoch).
How do I measure how long code takes?
Capture Date.now() before and after and subtract: const ms = Date.now() - start. For sub-millisecond precision use performance.now().
What is the difference between Date.now() and new Date()?
Date.now() returns a number (a timestamp); new Date() returns a Date object you can read parts from and format.
How do I convert a timestamp to a date?
Pass it to the constructor: new Date(timestamp).