Parameters
The getTime() method does not take any parameters. It is a parameterless method that simply retrieves the numeric time value associated with the Date object.
Return Type
The getTime() method returns a numeric value representing the number of milliseconds between the Unix Epoch (January 1, 1970, 00:00:00 UTC) and the specified date.
Description
The JavaScript Date getTime() Method is a part of the `Date` object. It is used to get the number of milliseconds that have passed since January 1, 1970, 00:00:00 UTC (Coordinated Universal Time). This specific date is known as the Unix epoch, & it serves as a reference point for time calculations in programming.
The `getTime()` method is straightforward to use. When you create a `Date` object, you can call this method to retrieve the timestamp in milliseconds. This timestamp is useful for various tasks, like comparing dates, calculating time differences, or storing time-related data in databases.
Let’s take a look at a simple example to show how `getTime()` works:
// Create a new Date object
const currentDate = new Date();
// Use the getTime() method to get the timestamp
const timestamp = currentDate.getTime();
// Output the result
console.log("Current Timestamp in Milliseconds:", timestamp);

You can also try this code with Online Javascript Compiler
Run Code
In this example:
1. We create a `Date` object called `currentDate`. By default, this object holds the current date & time.
2. We call the `getTime()` method on this object, which returns the number of milliseconds since the Unix epoch.
3. Finally, we log the result to the console.
The output will be a large number representing the current time in milliseconds. For example, it might look something like this:
Current Timestamp in Milliseconds: 1698765432100
This number is not human-readable, but it’s extremely useful for calculations & comparisons in programming.
Key Points
- If the Date object represents a valid date, the method returns a positive or negative number based on the time difference.
- If the Date object is invalid, the method returns NaN (Not-a-Number).
Examples
Let's look at some examples to understand how the JavaScript Date getTime() Method works:
Example 1: Basic Usage of getTime
const now = new Date();
const timeInMilliseconds = now.getTime();
console.log("Current time in milliseconds since January 1, 1970:", timeInMilliseconds);
Output:
Current time in milliseconds since January 1, 1970: 1672531200000
Explanation: This code creates a new Date object for the current date and time. It then calls getTime() to retrieve the time in milliseconds since the Unix Epoch.
Example 2: Calculating the Time Difference Between Two Dates
const date1 = new Date("2023-01-01T00:00:00Z");
const date2 = new Date("2025-01-01T00:00:00Z");
const diffInMilliseconds = date2.getTime() - date1.getTime();
const diffInDays = diffInMilliseconds / (1000 * 60 * 60 * 24);
console.log("Difference in milliseconds:", diffInMilliseconds);
console.log("Difference in days:", diffInDays);
Output:
Difference in milliseconds: 63115200000
Difference in days: 730
Explanation: This example calculates the time difference between two dates in both milliseconds and days. The time difference in milliseconds is divided by the number of milliseconds in a day to convert it to days.
Example 3: Checking If a Date is in the Past or Future
const eventDate = new Date("2024-12-31T23:59:59Z");
const currentTime = new Date().getTime();
if (eventDate.getTime() > currentTime) {
console.log("The event is in the future.");
} else {
console.log("The event has already passed.");
}
Output:
The event is in the future.
Explanation: The code compares the time value of an event date with the current time to determine if the event is upcoming or has passed.
Supported Browsers
The getTime() method is supported by all major browsers and their versions, including:
- Google Chrome
- Mozilla Firefox
- Microsoft Edge
- Safari
- Opera
Frequently Asked Questions
What does the getTime() method return?
The getTime() method returns the number of milliseconds between the Unix Epoch (January 1, 1970) and the specified date in the Date object.
Can getTime() return a negative value?
Yes, getTime() can return a negative value if the Date object represents a date before January 1, 1970.
What happens if the Date object is invalid?
If the Date object is invalid, the getTime() method returns NaN (Not-a-Number).
Conclusion
The JavaScript Date getTime() Method is a powerful tool for handling date and time values. It allows developers to retrieve the numeric representation of a date in milliseconds, making it easier to perform operations like calculating time differences or scheduling tasks.