JavaScript Get Date Difference in Seconds

In this tutorial we will learn how to Get Date Difference in Seconds with JavaScript. JavaScript new Date() constructor, Math.abs() method and Math.ceil() method are used to get the date difference in seconds.

JavaScript new Date() Constructor

JavaScript new Date() Constructor is used to create a date object which represents current date and time.

The date object returned by new Date() Constructor has current time in milliseconds since 1st January 1970 UTC till current date.

JavaScript Math.abs() method

JavaScript Math.abs() method returns the absolute value of the number.

JavaScript Math.ceil() method

JavaScript Math.ceil() method always rounds up a number to the next integer.

HTML Code

HTML Code is given below, in this code we have used input date elements to get two dates from user while a Button tag is used to execute a JavaScript function which will calculate date difference in seconds.

<input type="date" id="day1">
<input type="date" id="day2">
<button onclick="calculateSeconds()">Get Difference</button>   
<p id="output"></p> 

JavaScript Code

Take a look at the JavaScript code, getElementById() method is used to read the date from both input date elements. new Date constructor is used to get the date object in string format. Math.abs is used to get the absolute value of difference of two dates while Math.ceil is used to get exact date difference in seconds.

<script>
function calculateSeconds() {
var day1 = document.getElementById("day1").value;
var day2 = document.getElementById("day2").value;    
const dateOne = new Date(day1);
const dateTwo = new Date(day2);
const time = Math.abs(dateTwo - dateOne);
const seconds = Math.ceil(time / 1000);
document.getElementById("output").innerHTML=seconds + ' Seconds';    
}
</script>

Demo

Video Tutorial

Watch video tutorial on Get Date Difference in Seconds in JavaScript.

Change font-size using JavaScript Get Font Size of any Tag using JavaScript Change Position of HTML Element using JavaScript How to Change Image on Hover with JavaScript How to Disable Button by Class Name in JavaScript How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using JavaScript How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript