JavaScript Reference ยท Date and Intl
Date.prototype.getTime()
Reference for JavaScript Date.prototype.getTime(): read a date as milliseconds since the Unix epoch, compare dates, sort them and detect invalid dates.
getTime() returns the number stored inside a Date: the milliseconds since midnight UTC on January 1, 1970. Because it is a plain number, it is the right tool for comparing dates, finding the distance between them, and checking whether a date is valid.
The value is independent of time zones. Two Date objects that represent the same instant always have the same getTime(), even if they are displayed differently.
const d = new Date("2026-03-15T09:30:00Z"); d.getTime(); // 1773567000000
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
date.getTime()
Parameters: none.
Return value: an integer number of milliseconds since the Unix epoch (negative for dates before 1970), or NaN if the date is invalid.
Exceptions: a TypeError if called on something that is not a Date.
Usage notes
- Never compare dates with
===. Two different objects are never strictly equal. Comparea.getTime() === b.getTime(). The relational operators<and>work directly because dates convert to numbers. valueOf()returns the same number, and so does the unary plus (+date) and subtraction (b - a). UsinggetTime()just makes the intent clear.- Detecting invalid dates:
Number.isNaN(date.getTime()). A date that fails to parse still has the typeDate, soinstanceof Datealone is not a check. - Sorting: the comparison function
(a, b) => a - bworks because dates subtract to numbers. - Not affected by the time zone. For the local year, hour and so on, use the other getters.
- Seconds are not milliseconds. Unix time in many APIs is in seconds. Divide
getTime()by 1000 when you send it there. - Range: a date can represent about 270,000 years on either side of 1970. Beyond that the time value becomes
NaN. - Getting "now" is cheaper with
Date.now()than withnew Date().getTime().
Browser support
getTime() works in every browser and runtime.