How to get the number of seconds between two Dates in JavaScript?


In this tutorial, we will learn how to get the number of seconds between two dates with JavaScript.

There are different methods for checking the number of days, hours, and seconds that are commonly used to provide information related to data. Since you can’t manually change the count of the years and months, we follow some simple tricks to get the number in JavaScript.

Using the Date getTime() Method

In JavaScript, we use different methods to calculate the days, hours, and seconds. Most popular way to calculate the time is .getTime(). However, you will get the result in milliseconds and have to convert it into seconds through division.

Syntax

var x = new Date("Aug 12, 2022 19:45:25");
var y = new Date("Aug 14, 2022 19:45:25");
let seconds = Math.abs(x.getTime() - y.getTime())/1000;

Here x and y are two dates. We will use the getTime() to get the times in milliseconds of both dates. We take the absolute difference between the two times in milliseconds. Then, after subtracting the time in milliseconds, we will divide it by 1000.

Algorithm

  • STEP 1 − Create two dates using the new Date(). You can refer to the example for the format of a date.
  • STEP 2 − Use the .getTime() method to get the time in milliseconds of both dates.
  • STEP 3 − Subtract the old date from the recent date. Then, divide the output by 1000 to get the difference in seconds.
  • STEP 4 − Use the innerHTML method to check the number of seconds difference between the two dates

Example 1

We have created both the dates manually using the new Date(). You can also use different methods to generate the date in JavaScript.

<html> <body> <h4> Using <i> .getTime() method </i> to get number of seconds between to dates. </h4> <p id="date1"> </p> <p id="date2"> </p> <p id="seconds">Number of seconds between two dates: </p> <script> var t1 = new Date("Aug 12, 2022 19:45:25"); var t2 = new Date("Aug 14, 2022 19:45:25"); var dif = Math.abs(t1.getTime() - t2.getTime()) / 1000; document.getElementById('date1').innerHTML = "First date: " + t1; document.getElementById('date2').innerHTML = "Second date: " + t2; document.getElementById('seconds').innerHTML += dif + " seconds"; </script> </body> </html>

Using the Math.abs() Method

Math.abs() is a method used to round the numbers to the nearest answer to avoid getting floating numbers. Hence, you can also use Math.round() method instead of Math.abs(). You can get the time difference using this method in milliseconds. The time can be converted in seconds by dividing the output by 1000.

Syntax

We will use the following syntax to get the time in milliseconds and convert it into seconds through the division method

let x = new Date();
let y = new Date();
let dif = Math.abs(x - y) / 1000;

Here, we find the number of seconds between two dates – x and y. We take the absolute difference between these two dates using the Math.abs() method. This difference is in milliseconds, so we divide by 1000 to convert it into seconds.

Example 2

In this example, there are more than 31 days of difference in both the dates. We kept the dates similar so that the output could also be checked in seconds manually.

<html> <head> <title> Example - get seconds between two dates </title> <head> <body> <h3> Get seconds between to dates using absolute difference of the dates </h3> <p id="date1"> </p> <p id="date2"> </p> <p id="seconds">Number of seconds between two dates: </p> <script> let date1 = new Date("Nov 25 2022 07:24:35"); let date2 = new Date(); var dif = Math.abs(date1 - date2) / 1000; document.getElementById('date1').innerHTML = "First date: " + date1; document.getElementById('date2').innerHTML = "Second date: " + date2; document.getElementById('seconds').innerHTML += dif; </script> </body> </html>

The methods like .getTime() and Math.abs() are commonly used in JavaScript. You can alternatively use the Math.round() method to get similar output.

Have a look at the below example.

Example 3

<html> <head> <title> Example - get seconds between two dates </title> <head> <body> <h3> Get seconds between to dates using Matth.round() method </h3> <p id="date1"> </p> <p id="date2"> </p> <p id="seconds">Number of seconds between two dates: </p> <script> let date1 = new Date("Nov 25 2022 07:24:35"); let date2 = new Date(); var dif = Math.round(date1 - date2) / 1000; document.getElementById('date1').innerHTML = "First date: " + date1; document.getElementById('date2').innerHTML = "Second date: " + date2; document.getElementById('seconds').innerHTML += dif; </script> </body> </html>

While getting the answers, you should ensure the time is converted in seconds because the output can also be in minutes or milliseconds. Hence, you can check the examples above to understand the best way to get the output in seconds.

Updated on: 15-Sep-2022

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements