Get time duration between 2 dates in java script

In this article, we will show you how to get the time difference between to dates in jQuery.
For this, you need 2 dates From date and To dates. We will create a common function,  you just need to add the from date and to date in it then it will show you the result in an array. You can use this function anywhere in your code.

Get time duration between 2 dates in java script


<html>
<head>
<title>Get time duration between 2 dates in javascript</title>
</head>
<body>
<script>
function timeDuration(from_times,to_times){
document.write(“”+from_times);
document.write(“<br>”+to_times);
var res = Math.abs(from_times – to_times) / 1000;
// get total days
var days = Math.floor(res / 86400);
document.write(“<br>Difference (Days): “+days);
// get total hours
var hours = Math.floor(res / 3600) % 24;
document.write(“<br>Difference (Hours): “+hours);
// get total minutes
var minutes = Math.floor(res / 60) % 60;
document.write(“<br>Difference (Minutes): “+minutes);
// get total seconds
var seconds = res % 60;
document.write(“<br>Difference (Seconds): “+seconds);
}
var from_times, to_times;
from_times = new Date( “Jan 11, 2020 11:10:05” );
to_times = new Date( “Fab 29, 2020 08:15:10” );
timeDuration(from_times,to_times);
</script>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*