getTime() returns the timestamp stored by a JavaScript Date: milliseconds since 1 January 1970 00:00:00 UTC. Dates before the epoch return negative values.
const date = new Date();
const milliseconds = date.getTime();
console.log(milliseconds);
Timestamps are useful for comparing dates, measuring elapsed time, copying Date objects, and storing an instant independently of display format.
const timestamp = date.getTime();
const epoch = new Date(0);
const beforeEpoch = new Date(-1000);
const a = new Date("2026-09-10T10:00:00Z");
const b = new Date("2026-09-11T10:00:00Z");
console.log(a.getTime() < b.getTime());
const dates = [b, a];
dates.sort((x, y) => x.getTime() - y.getTime());
const start = new Date("2026-09-10T10:00:00Z");
const end = new Date("2026-09-10T10:02:30Z");
const seconds = (end.getTime() - start.getTime()) / 1000;
console.log(seconds); // 150
The original tutorial also uses timestamps to calculate process duration; see the process-time demo.
const original = new Date();
const copy = new Date(original.getTime());
const fromObject = (new Date()).getTime();
const directly = Date.now();
Use Date.now() when you only need the current timestamp and do not need a Date object.
const bad = new Date("invalid");
console.log(bad.getTime()); // NaN
Date.now() and getTime() represent wall-clock time. For measuring short code execution intervals in a browser, performance.now() provides a monotonic, higher-resolution timer.
const start = performance.now();
// work to measure
const elapsed = performance.now() - start;
NaN for invalid dates.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.