Nodejs中的时差?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16495368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:28:33  来源:igfitidea点击:

Time difference in Nodejs?

node.js

提问by Alosyius

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

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

getTime()

获取时间()

function not being found

找不到函数

回答by Noah

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.jsmodule

您将需要moment.js模块

npm install -S moment

回答by rogierschouten

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":

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

回答by Daniyal

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)
}`

回答by aswin prabhakar

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.

如果您只想像我一样在循环上设置超时,则根本不需要使用 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: 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'.

PS:我知道这篇文章并没有完全回答所提出的问题,但这是当您在 google 上搜索“nodejs 中的时差”时弹出的链接。

回答by Shaikh Shahid

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

回答by K.Suthagar

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 momentand want to import before using.

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

回答by M. Hamza Rajput

Moment.js

时刻.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'));