获取两个日期时间之间的时差 [英] Get the time difference between two datetimes
问题描述
我知道我可以使用 momentjs 做任何事情,并且可以做一些更复杂的 Dates.但令人尴尬的是,我很难尝试做一些看起来很简单的事情:计算 2 次之间的差异.
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.
示例:
var now = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";
//expected result:
"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"))
//outputs 10:39:30
我不明白那里的10"是什么.我住在巴西,所以如果相关,我们是 utc-0300.
I do not understand what is that "10" there. I live in Brazil, so we are utc-0300 if that is relevant.
moment.duration(now.diff(then))
的结果是具有正确内部值的持续时间:
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
所以,我想我的问题是:如何将 momentjs Duration 转换为时间间隔?我肯定可以使用
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.
更新
仔细看,在上面的例子中 now
是:
Tue Apr 09 2013 15:00:00 GMT-0300 (E. South America Standard Time)…}
和 moment(moment.duration(now.diff(then)))
是:
Wed Dec 31 1969 22:39:30 GMT-0200 (E. South America Daylight Time)…}
我不确定为什么第二个值是夏令时 (-0200)...但我确定我不喜欢日期 :(
I am not sure why the second value is in Daylight Time (-0200)... but I am sure that i do not like dates :(
更新 2
嗯,值是 -0200 可能是因为 31/12/1969 是使用夏令时的日期......就是这样.
well, the value is -0200 probably because 31/12/1969 was a date where the daylight time was being used... so thats that.
推荐答案
此方法仅在总持续时间少于 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")
// outputs: "00:39:30"
如果您有 24 小时或更长时间,则使用上述方法将小时数重置为零,因此并不理想.
If you have 24 hours or more, the hours will reset to zero with the above approach, so it is not ideal.
如果您想获得 24 小时或更长时间的有效响应,那么您必须执行以下操作:
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"
请注意,我使用 UTC 时间作为快捷方式.您可以分别拉出 d.minutes()
和 d.seconds()
,但您还必须对它们进行零填充.
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.
这是必要的,因为目前在 moment.js 中没有格式化 duration
反对意见的功能.已在此处请求.但是,有一个名为 moment-duration-format 的第三方插件专门用于这个目的:
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屋!