Reputation: 9111
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
Upvotes: 22
Views: 60888
Reputation: 443
If you just want to set timeout on a loop as I did, you don't need to use momentjs at all. Try the following.
let timeout = 3; //seconds
console.log("start:" + new Date());
let start = Number(Date.now());
let end = start + timeout * 1000;
while (Number(Date.now()) < end) {
// your code here
}
console.log("end:" + new Date());
PS: I understand that this post does not quite answer the question asked, but this is the link popping up when you google 'time difference in nodejs'.
Upvotes: 2
Reputation: 10226
const MOMENT = require('moment');
const TimeDiff = ( startTime, endTime, format ) => {
startTime = MOMENT( startTime, 'YYYY-MM-DD HH:mm:ss' );
endTime = MOMENT( endTime, 'YYYY-MM-DD HH:mm:ss' );
return endTime.diff( startTime, format);
}
let startTime = new Date('2013-5-11 8:37:18');
let endTime = new Date('2013-5-11 10:37:18');
console.log( TimeDiff(startTime, endTime, 'seconds'));
Upvotes: 0
Reputation: 790
It will check the system time and find the difference.
var moment = require('moment')
var firstTime = moment();
setInterval(someTask,1000);
function someTask(){
var secondTime = moment();
var timeDifference = secondTime.diff(firstTime, 'seconds')
console.log(timeDifference)
}`
Upvotes: 3
Reputation: 2306
We can use this optimized code too,
function calculateDays(startDate,endDate)
{
var start_date = moment(startDate, 'YYYY-MM-DD HH:mm:ss');
var end_date = moment(endDate, 'YYYY-MM-DD HH:mm:ss');
var duration = moment.duration(end_date.diff(start_date));
var days = duration.asDays();
return days;
}
Here you need to install moment
and want to import before using.
Upvotes: 1
Reputation: 1215
Get the remaining days :
var moment = require('moment');
var startDate = moment(new Date()).format("YYYY-MM-DD");
var endDate = moment(new Date("Tue Aug 27 2015 09:13:40 GMT+0530 (IST)")).format("YYYY-MM-DD");
var remainingDate = moment(endDate).diff(startDate, 'days');
console.log(remainingDate) // at time of posting, 106 days
Upvotes: 1
Reputation: 153
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":
var tc = require("timezonecomplete");
var start = new tc.DateTime("2014-06-26T12:00:00 Europe/Amsterdam");
var end = new tc.DateTime("2014-06-26T12:00:00 UTC");
var duration = end.diff(start); // unit-aware duration
console.log(duration.minutes()); // -120
console.log(duration.hours()); // -2
Upvotes: 4
Reputation: 34313
var moment = require('moment')
var startDate = moment('2013-5-11 8:73:18', 'YYYY-M-DD HH:mm:ss')
var endDate = moment('2013-5-11 10:73:18', 'YYYY-M-DD HH:mm:ss')
var secondsDiff = endDate.diff(startDate, 'seconds')
console.log(secondsDiff)
You will need the moment.js module
npm install -S moment
Upvotes: 40