Calculate difference between 2 timestamps using javascript
Based on the approved answer:
function(timestamp1, timestamp2) {
var difference = timestamp1 - timestamp2;
var daysDifference = Math.floor(difference/1000/60/60/24);
return daysDifference;
}
i am posting my own example try implement this in your code
function timeDifference(date1,date2) {
var difference = date1.getTime() - date2.getTime();
var daysDifference = Math.floor(difference/1000/60/60/24);
difference -= daysDifference*1000*60*60*24
var hoursDifference = Math.floor(difference/1000/60/60);
difference -= hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference -= minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000);
console.log('difference = ' +
daysDifference + ' day/s ' +
hoursDifference + ' hour/s ' +
minutesDifference + ' minute/s ' +
secondsDifference + ' second/s ');
}
A better alternative would be using window.performance API.
const startTime = window.performance.now()
setTimeout(()=>{
const endTime = window.performance.now()
console.log("Time Elapsed : ",endTime-startTime) // logs ~2000 milliseconds
}, 2000)