Timestamps, .now( ), and .valueOf ( )
A timestamp is a moment in time written as a single number. JavaScript counts milliseconds since midnight UTC on 1 January 1970, the Unix epoch, which is also how a Date object stores its value internally. A timestamp is the same in every time zone, so the examples on this page print the same output wherever they run.
Reading a date’s timestamp
Section titled “Reading a date’s timestamp”A Date object’s valueOf() and getTime() methods both return its timestamp. JavaScript calls valueOf() itself whenever a date is converted to a number, so the unary + and Number() give the same result.
const order = new Date('2025-03-14T09:30:00Z');
console.log(order.valueOf());console.log(order.getTime());console.log(+order);console.log(Number(order));This prints:
1741944600000174194460000017419446000001741944600000getTime() says what it does, so it is the clearer choice in code that others will read.
The current time with Date.now()
Section titled “The current time with Date.now()”Date.now() is a static method, which means it is called on Date itself rather than on a date object. It returns the current timestamp without creating a Date object, so it is shorter and cheaper than new Date().getTime(). Its value changes on every call, but it is always a whole number of milliseconds.
const now = Date.now();
console.log(typeof now, Number.isInteger(now));console.log(new Date(now) instanceof Date);This prints:
number truetruevalueOf() and getTime() are instance methods by contrast, because they read the value of one particular date object.
Measuring elapsed time
Section titled “Measuring elapsed time”Subtracting one timestamp from another gives the time between them in milliseconds, which is a quick way to time an operation.
const start = Date.now();
setTimeout(() => { const elapsed = Date.now() - start; console.log(`Took about ${Math.round(elapsed / 100) / 10} s`);}, 200);This prints:
Took about 0.2 sDate.now() follows the system clock, which can be adjusted while a program runs. For precise measurements, use performance.now(), which counts from the start of the page or process, never goes backwards, and has sub-millisecond precision.
Comparing and subtracting dates
Section titled “Comparing and subtracting dates”Arithmetic and comparison operators convert dates to timestamps automatically, so subtracting two dates gives the gap in milliseconds, and < and > work as expected.
const placed = new Date('2025-03-14T09:30:00Z');const delivered = new Date('2025-03-17T15:45:00Z');
const ms = delivered - placed;const hours = ms / (1000 * 60 * 60);
console.log(ms);console.log(hours);console.log(delivered > placed);This prints:
28170000078.25trueEquality is the exception, because === and == compare two objects by reference. Two separate date objects for the same moment are not equal, so compare their timestamps instead.
const a = new Date('2025-03-14T09:30:00Z');const b = new Date('2025-03-14T09:30:00Z');
console.log(a === b);console.log(a.getTime() === b.getTime());This prints:
falsetrueSeconds and milliseconds
Section titled “Seconds and milliseconds”Many other systems, such as Unix tools, databases and some web APIs, count seconds since the epoch rather than milliseconds. Multiply by 1000 to turn one of those into a JavaScript date, and divide by 1000 to go the other way.
const seconds = 1741944600;const fromUnix = new Date(seconds * 1000);
console.log(fromUnix.toISOString());console.log(Math.floor(fromUnix.getTime() / 1000));This prints:
2025-03-14T09:30:00.000Z1741944600A timestamp from an API that is roughly a thousand times too small, giving a date in January 1970, is the usual sign that the units were mixed up. Time and Dates covers creating and formatting dates.
Steve Griffith covers this topic in his video Timestamps, Date.now( ) and valueOf( ).