Im trying to figure out a way to get the time difference in seconds between two dates.
For example, difference in seconds between:
2013-5-11 8:37:18
2013-5-11 10:37:18
Tried:
function timeDifference(laterdate, earlierdate) {
var difference = laterdate.getTime() - earlierdate.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);
return secondsDifference;
}
But that does not work in Nodejs, error with
getTime()
function not being found
Moment.js
You will need the moment.js module
It will check the system time and find the difference.
The timezonecomplete module has support for date differences, even with dates in different time zones. It returns a Duration object which is unit-aware, not just a "number" that represents "milliseconds":
We can use this optimized code too,
Here you need to install
moment
and want to import before using.Get the remaining days :