The JavaScript getTime() function is one of the date functions, which returns the time (timestamp) from a given date. This getTime function returns the total number of Milliseconds from January 1, 1970, 00:00:00 UTC to a given datetime.
JavaScript getTime() syntax
The syntax of the getTime() function is
dateObject.getTime()
As you can see, the getTime() function does not take any parameters. It simply needs a date object to get the time value in milliseconds.
Return Value: As the name suggests, it returns the time value from the given date. However, the time is returned as a number (integer) representing the timestamp in milliseconds. If the given date is invalid, the getTime() function returns NaN as output.
Use JavaScript getTime to get the current timestamp
In the following JavaScript example, the first line, new Date(), returns the current date and time. Next, we are using the getTime() function to return the number of Milliseconds from the default date to the current datetime.
const dt = new Date()
const t = dt.getTime()
console.log(`Current Date and Time = ${dt}`)
console.log(`Current Timestamp = ${t}`)
Current Date and Time = Wed Jun 03 2026 10:46:08 GMT+0530 (India Standard Time)
Current Timestamp = 1780463768343
Get the timestamp from the custom date and time
Similar to the above, in this example, we use the JavaScript getTime function on the custom date. Here, we built the custom date and time using the Date() function and then used the getTime() to extract the timestamp in milliseconds.
<!DOCTYPE html>
<html>
<head>
<title> JavaScriptGetTimeFunction </title>
</head>
<body>
<h1> JavaScriptgetTimeFunction Example </h1>
<script>
var dt = Date("December 22, 2002 10:11:43");
document.write("Date and Time : " + dt);
document.write("Time using getTime(): " + dt.getTime());
</script>
</body>
</html>

Get timestamp from Date without Time
In this example, we use the JavaScript getTime() function on the custom date without the time. This function considers the time 00:00:00 and returns the milliseconds from the default time (1 January 1970) to the specified date (22 December 1972).
const dt = new Date("December 22, 1972");
const t = dt.getTime()
console.log(`Current Date and Time = ${dt}`)
console.log(`Current Timestamp = ${t}`)
Current Date and Time = Fri Dec 22 1972 00:00:00 GMT+0530 (India Standard Time)
Current Timestamp = 93810600000
JavaScript getTime in seconds
Generally, the getTime() function returns the current timestamp in milliseconds. So, we must divide the resultant milliseconds by 1000 to get the current time in seconds.
let dt = new Date();
let tm = dt.getTime();
let t = Math.floor(tm / 1000);
console.log("Milliseconds = ", tm);
console.log("Seconds = ", t);
Milliseconds = 1781249658814
Seconds = 1781249658
JavaScript getTime() real-time examples
Difference between two dates
We can use the getTime() function to compare two date objects and find the difference between those dates in milliseconds.
const start = new Date("January 1, 2026");
const end = new Date("June 3, 2026");
const diff = end.getTime() - start.getTime();
console.log(`Time Difference = ${diff}`)
Time Difference = 13219200000
Use the code below to show the difference in days. Here, 1000 milliseconds = 1 second, 60 seconds * 60 minutes * 24 hours.
const days = diff / (1000 * 60 * 60 * 24);
console.log(days);
153
Calculate the age
Use the above technique and change the start date to the birth date to check the age.
const start = new Date('January 1, 1990');
const diff = Date.now() - start.getTime();
const age = Math.round(diff / (1000 * 60 * 60 * 24 * 365));
console.log(age);
36
Convert the timestamp to a datetime
If you have the timestamp (returned by the JavaScript getTime() or from another source), we can use the Date() function to convert it to a date and time.
const date = new Date(1780463768343);
console.log(date.toString());
Wed Jun 03 2026 10:46:08 GMT+0530 (India Standard Time)
Check if a date is in the past or future
It is helpful to check whether the date is in the future or the past. For instance, in a subscription app, if the subscription expiry date is less than the current date, the app should not work. Please refer to the If Else statement.
const expiryDt = new Date('2026-05-31');
if (expiryDt.getTime() < Date.now())
{
console.log('Subscription Expired');
}
else {
console.log('Subscription Still active');
}
Subscription Expired
Similarly, if you change the date to December 31, 2026, it is a future date.
Use JavaScript getTime() to Measure the elapsed time
If we are executing a time consuming tasks or to measure the time taken to execute a specific code block, we can use the getTime() function. In the following example, we use the getTime() to extract the current timestamp as the starting value.
For the task simulation, we used a for loop to iterate from 0 to 10000, and inside it, we used the exp() to find the exponent of each value. After the for loop, we again use the getTime() to get the time after the task completes. Next, subtract the end time from the start position to measure the elapsed time of the task.
const start = new Date().getTime();
for (let i = 0; i < 10000; i++)
{
Math.exp(i);
}
const end = new Date().getTime();
console.log('Execution time:', end - start, 'ms');
Execution time: 1 ms
Passing an invalid date to the getTime() function
When we pass an invalid date to the getTime() function, it will return NaN as output.
const dt = new Date('20260604')
const t = dt.getTime();
console.log(t);
NaN
Date.now() vs new Date().getTime()
If your goal is to extract the timestamp in milliseconds from 1970 to the current date and time, both will return an identical result.
- The Date.now() can be used only when we are working with the current date. It is faster for the current date.
- However, getTime() can be used on the current date and the custom dates. If we already have the date object with custom date values, use the getTime() method.
console.log(Date.now());
console.log(new Date().getTime());
1780467128579
1780467128583
Can getTime() return negative numbers?
Yes. When we pass the dates before January 1, 1970, the getTime() function returns the timestamp in negative numbers.
const dt = new Date('December 31, 1969');
const t = dt.getTime();
console.log(t);
-106200000
Countdown Timer Example
Please refer to the floor function in the math library.
const eventDate = new Date('2026-06-04').getTime();
const timer = setInterval(() =>
{
const now = Date.now();
const remaining = eventDate - now;
if (remaining <= 0) {
clearInterval(timer);
console.log('Mobile Phone Event Launch Started');
return;
}
const seconds = Math.floor(remaining / 1000);
console.log(`${seconds} seconds left..`);
}, 1000);
63688 seconds left..
63687 seconds left..
63686 seconds left..
63685 seconds left..