getters
Date.prototype.getTime
Returns the underlying timestamp of the date as milliseconds since the Unix epoch (1970-01-01T00:00:00Z). This is the canonical way to compare, subtract, or serialize a Date as a number.
date.getTime() Parameters
| Parameter | Purpose |
|---|---|
| () | no arguments; returns Number (integer ms) |
| valueOf() | identical result; called implicitly by arithmetic operators |
Examples
console.log(new Date('2026-07-19T00:00:00Z').getTime()); // 1784332800000 epoch ms
console.log(new Date('2026-07-20') - new Date('2026-07-19')); // 86400000 diff in ms via subtraction
const a = new Date(), b = new Date(a.getTime()); console.log(a.getTime() === b.getTime()); // true clone by timestamp
Gotcha
Returns NaN for an Invalid Date (e.g. new Date('nope').getTime()). Prefer getTime() over Number(date) for readability.
Related
Date.now
Returns the current time as an integer number of milliseconds since the Unix epoch without allocating a Date object. Use it for timestamps, expiry checks, and simple timing.
new Date()
Creates a Date object representing a single moment in time as milliseconds since the Unix epoch (1970-01-01T00:00:00Z). Called with no arguments it returns the current instant; with arguments it parses a timestamp, ISO string, or component parts.
Date.prototype.toJSON
Returns an ISO 8601 UTC string suitable for JSON serialization; called automatically by JSON.stringify when it encounters a Date. Equivalent to toISOString() for valid dates, but returns null for Invalid Dates instead of throwing.
Date.prototype.getFullYear
Returns the 4-digit year of the date according to LOCAL time. Prefer this over the deprecated getYear(), which returns year-1900.
Date.prototype.getMonth
Returns the month of the date in LOCAL time as an integer from 0 (January) to 11 (December). This zero-based indexing is the single most common source of off-by-one date bugs in JavaScript.