Date.prototype.getTime
Returns the number of milliseconds since January 1, 1970 00:00:00 UTC for the given date
Syntax
date.getTime()Return Value
A number representing milliseconds since epoch
Examples
const date = new Date('2024-01-15');
console.log(date.getTime()); // 1705276800000const d1 = new Date('2024-01-01');
const d2 = new Date('2024-12-31');
const diffDays = (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24);
console.log(diffDays); // 365const date = new Date();
const copy = new Date(date.getTime());
console.log(date.getTime() === copy.getTime()); // trueUnderstanding Date.prototype.getTime
The Date.prototype.getTime method in JavaScript returns the number of milliseconds since January 1, 1970 00:00:00 UTC for the given date. It belongs to the Date object and is one of the most widely used methods for working with date values in modern JavaScript and TypeScript applications.
The method signature is date.getTime(). When called, it returns a number representing milliseconds since epoch. Understanding when and how to use getTime() helps you write more expressive, readable code.
Common use cases for Date.prototype.getTime include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like date-now, date-parse, date-toisostring, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Date.prototype.getTime is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Date.nowReturns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC
Date.parseParses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC
Date.prototype.toISOStringReturns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long
More Date Methods
Other methods in the Date object
Related Tools
More Date Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.