21

我试图找出一种方法来获取两个日期之间的时间差(以秒为单位)。

例如,以秒为单位的差异:

2013-5-11 8:37:18

2013-5-11 10:37:18

试过:

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;
}

但这在Nodejs中不起作用,错误

获取时间()

找不到函数

4

7 回答 7

37
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)

您将需要moment.js模块

npm install -S moment
于 2013-05-11T13:15:46.053 回答
4

timezonecomplete 模块支持日期差异,即使日期在不同时区。它返回一个单位感知的 Duration 对象,而不仅仅是表示“毫秒”的“数字”:

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
于 2014-06-26T16:23:33.500 回答
2

它将检查系统时间并找出差异。

var moment = require('moment')
var firstTime = moment();

setInterval(someTask,1000);
function someTask(){
  var secondTime = moment();
  var timeDifference = secondTime.diff(firstTime, 'seconds')
  console.log(timeDifference)
}`
于 2019-05-18T12:43:51.703 回答
2

如果您只想像我一样在循环上设置超时,则根本不需要使用 momentjs。试试下面的。

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:我知道这篇文章并没有完全回答所提出的问题,但这是当你谷歌“nodejs 中的时差”时弹出的链接。

于 2020-05-07T05:23:58.070 回答
1

获取剩余天数:

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
于 2015-05-13T09:18:44.290 回答
1

我们也可以使用这个优化的代码,

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;
}

这里需要安装moment并想导入才能使用。

于 2017-12-21T15:00:03.600 回答
0

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'));
于 2019-09-20T11:24:00.797 回答