Im trying to get the number of seconds between two dates in javascript.
Questions here and here suggest that a simple subtract operation should do the trick and other answers suggest that you use either the .getTime()
or .valueOf()
attributes.
But Despite all that when I subtract the two dates, Which are apart by 5 seconds I get the value 1551181475795
which is way too large to be correct.
Here is my code:
var countDownDate = new Date("Mar 16, 2019 16:33:25").valueOf();
var dist = 347155200;
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date("Mar 16, 2019 16:33:30");
// Find the distance between now and the count down date
var distance = Math.abs(now - countDownDate / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = distance;
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
<p id='demo'>
As you can see both times are only apart by 5 seconds but the value that separates them makes no sense.
Thanks in Advance!