Time difference in Nodejs?

2020-02-09 07:07发布

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

标签: node.js
6条回答
够拽才男人
2楼-- · 2020-02-09 07:17

Moment.js

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'));
查看更多
叼着烟拽天下
3楼-- · 2020-02-09 07:24
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
查看更多
虎瘦雄心在
4楼-- · 2020-02-09 07:32

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)
}`
查看更多
放荡不羁爱自由
5楼-- · 2020-02-09 07:33

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
查看更多
手持菜刀,她持情操
6楼-- · 2020-02-09 07:34

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.

查看更多
小情绪 Triste *
7楼-- · 2020-02-09 07:38

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
查看更多
登录 后发表回答