Get the Number of Seconds between 2 Dates in JavaScript

Last updated: Jan 17, 2022

Photo from Unsplash

Get the Number of Seconds between 2 Dates in JavaScript#

To get the number of seconds between 2 dates:

1. Get the number of milliseconds between the unix epoch and the Dates.
2. Subtract the milliseconds of the start date from the milliseconds of the end date.
3. Divide the result by the number of milliseconds in a second (1000).
index.js
```Copied!```function getSecondsDiff(startDate, endDate) {
const msInSecond = 1000;

return Math.round(
Math.abs(endDate - startDate) / msInSecond
);
}

// 👇️ (1 min 30 seconds - 0 seconds = 90 seconds)
console.log(
getSecondsDiff(
new Date(2022, 1, 24, 9, 44, 0),
new Date(2022, 1, 24, 9, 45, 30),
),
);

// 👇️ (5 minutes - 0 seconds = 300 seconds)
console.log(
getSecondsDiff(
new Date(2022, 1, 24, 10, 40, 0),
new Date(2022, 1, 24, 10, 45, 0),
),
);
``````

The parameters we passed to the `Date()` constructor in the examples are - `year`, `month` (0 = January, 1 = February, etc), `day of the month`, `hour`, `minutes` and `seconds`.

We created a reusable function that returns the number of seconds between 2 dates.

The `msInSecond` variable stores the number of milliseconds there are in a second.

When you subtract a Date object from another Date object, they both get Implicitly converted to a number that represents the milliseconds elapsed between January 1st, 1970 and the given date.

We could have called the `getTime()` method on both of the `Date` objects to achieve the same result.

index.js
```Copied!```function getSecondsDiff(startDate, endDate) {
const msInSecond = 1000;

return Math.round(
Math.abs(endDate.getTime() - startDate.getTime()) / msInSecond,
);
}

// 👇️ (1 min 30 seconds - 0 seconds = 90 seconds)
console.log(
getSecondsDiff(
new Date(2022, 1, 24, 9, 44, 0),
new Date(2022, 1, 24, 9, 45, 30),
),
);

// 👇️ (5 minutes - 0 seconds = 300 seconds)
console.log(
getSecondsDiff(
new Date(2022, 1, 24, 10, 40, 0),
new Date(2022, 1, 24, 10, 45, 0),
),
);
``````

This time we called the `getTime()` method to explicitly convert the `Date` object to a timestamp when subtracting.

This might be the more clear and explicit approach for readers of your code.

The next step is to divide the result by the number of milliseconds in a second to convert the value from milliseconds to seconds.

We used the Math.abs function to handle a scenario where we subtract a greater timestamp from a smaller one.

The `Math.abs` function returns the absolute value of a number. In other words, if the number is positive, the number is returned and if the number is negative, the negation of the number is returned.

index.js
```Copied!```console.log(Math.abs(-100)); // 👉️ 100
console.log(Math.abs(100)); // 👉️ 100
``````

We passed the value to the Math.round function to round to the nearest integer.

Here are some examples of how the `Math.round` function works.

index.js
```Copied!```console.log(Math.round(1.49)); // 👉️ 1
console.log(Math.round(1.5)); // 👉️ 2
``````

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to `0.5`, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than `0.5`, it gets rounded to the lower absolute value.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
You can use the search field on my Home Page to filter through all of my articles.