获取两个数据时间之间的时差 [英] Get the time difference between two datetimes

查看:100
本文介绍了获取两个数据时间之间的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以做任何事情,还有一些更有活力的日子与momentjs。但是令人尴尬的是,我很难做一些似乎很简单的事情:在两次之间取得差异。



示例:

  var now =04/09/2013 15:00:00; 
var then =04/09/2013 14:20:30;

//预期结果:
00:39:30

我试过的:

  var now = moment(04/09/2013 15:00:00); 
var then = moment(04/09/2013 14:20:30);

console.log(moment(moment.duration(now.diff(then)))。format(hh:mm:ss))
//输出10:39:30

我不明白那里的10是什么。我住在巴西,所以我们是utc-0300,如果这是相关的。



moment.duration(now.diff(then) )是具有正确内部值的持续时间:

  days:0 
hours: 0
毫秒:0
分钟:39
个月:0
秒:30
年:0

所以,我想我的问题是:如何将一个momentjs持续时间转换为一个时间间隔?我确定可以使用

  duration.get(hours)+:+ duration.get(minutes)+ :+ duration.get(seconds)

但是我觉得还有更多的东西



更新

看起来更近,在上面的例子中 now 是:

  Tue Apr 09 2013 15:00:00 GMT-0300 (E.南美洲标准时间)... 

持续时间(now.diff(then)))是:

  Wed Dec 31 1969 22:39 :30 GMT-0200(E.南美洲夏令时间)... 

我不知道为什么第二个值是在日光时间(-0200)...但我相信我不喜欢日期:(



更新2 / p>

很好,值为-0200可能是因为31/12/1969是使用日光时间的日期...所以这样。

解决方案

当总持续时间少于24小时时,此方法将起作用:

  var now =04/09/2013 15:00:00; 
var then =04/09/2013 14:20:30;

moment.utc(moment(now,DD / MM / YYYY HH:mm:ss)diff(moment(then,DD / MM / YYYY HH:mm:ss)) ).format(HH:mm:ss)

//输出:00:39:30

如果您有24小时以上的时间,上述方法的时间将重置为零,所以不太理想。



如果您希望得到24小时或更长时间的有效回应,那么您必须这样做:

  var now =04/09/2013 15:00:00; 
var then =02/09/2013 14:20:30;

var ms = moment(现在,DD / MM / YYYY HH:mm:ss)diff(moment(then,DD / MM / YYYY HH:mm:ss));
var d = moment.duration(ms);
var s = Math.floor(d.asHours())+ moment.utc(ms).format(:mm:ss);

//输出:48:39:30

请注意我正在使用utc时间作为快捷方式。你可以分别拉出 d.minutes() d.seconds(),但你也必须使用zeropad



这是必要的,因为格式化持续时间异议的能力目前不在moment.js中。 已在此请求。但是,有一个名为时间长度格式的第三方插件专门用于此目的: p>

  var now =04/09/2013 15:00:00; 
var then =02/09/2013 14:20:30;

var ms = moment(现在,DD / MM / YYYY HH:mm:ss)diff(moment(then,DD / MM / YYYY HH:mm:ss));
var d = moment.duration(ms);
var s = d.format(hh:mm:ss);

//输出:48:39:30


I know I can do anything and some more envolving Dates with momentjs. But embarrassingly, I'm having a hard time trying to do something that seems simple: geting the difference between 2 times.

Example:

var now  = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";

//expected result:
"00:39:30"

what I tried:

var now = moment("04/09/2013 15:00:00");
var then = moment("04/09/2013 14:20:30");

console.log(moment(moment.duration(now.diff(then))).format("hh:mm:ss"))
//outputs 10:39:30  

I do not understand what is that "10" there. I live in Brazil, so we are utc-0300 if that is relevant.

The result of moment.duration(now.diff(then)) is a duration with the correct internal values:

 days: 0
 hours: 0
 milliseconds: 0
 minutes: 39
 months: 0
 seconds: 30
 years: 0

So, I guess my question is: how to convert a momentjs Duration to a time interval? I sure can use

duration.get("hours") +":"+ duration.get("minutes") +:+ duration.get("seconds")

but i feel that there is something more elegant that I am completely missing.

update
looking closer, in the above example now is:

Tue Apr 09 2013 15:00:00 GMT-0300 (E. South America Standard Time)…}

and moment(moment.duration(now.diff(then))) is:

Wed Dec 31 1969 22:39:30 GMT-0200 (E. South America Daylight Time)…}

I am not sure why the second value is in Daylight Time (-0200)... but I am sure that i do not like dates :(

update 2

well, the value is -0200 probably because 31/12/1969 was a date where the daylight time was being used... so thats that.

解决方案

This approach will work when the total duration is less than 24 hours:

var now  = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";

moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")

// outputs: "00:39:30"

If you have 24 hours or more, the hours will reset to zero with the above approach, so it is not ideal.

If you want to get a valid response for durations of 24 hours or greater, then you'll have to do something like this instead:

var now  = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";

var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

// outputs: "48:39:30"

Note that I'm using the utc time as a shortcut. You could pull out d.minutes() and d.seconds() separately, but you would also have to zeropad them.

This is necessary because the ability to format a duration objection is not currently in moment.js. It has been requested here. However, there is a third-party plugin called moment-duration-format that is specifically for this purpose:

var now  = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";

var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = d.format("hh:mm:ss");

// outputs: "48:39:30"

这篇关于获取两个数据时间之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆