在Moment Js中获取两个日期之间的小时差异 [英] Get hours difference between two dates in Moment Js
本文介绍了在Moment Js中获取两个日期之间的小时差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用MomentJs获得两个日期之间的区别,如下所示:
I'm able to get the difference between two dates using MomentJs as follows:
moment(end.diff(startTime)).format("m[m] s[s]")
然而,我也想在适用时显示小时数(仅当> = 60分钟过去时)。
However, I also want to display the hour when applicable (only when >= 60 minutes have passed).
但是,当我尝试使用以下内容检索持续时间时:
However, when I try to retrieve the duration hours using the following:
var duration = moment.duration(end.diff(startTime));
var hours = duration.hours();
它返回当前小时,而不是两个日期之间的小时数。
it is returning the current hour and not the number of hours between the two dates.
如何获得两个时刻之间的差距?
How do I get the difference in hours between two Moments?
推荐答案
你很近您只需要使用 duration.asHours()
方法(请参阅文档)。
You were close. You just need to use the duration.asHours()
method (see the docs).
var duration = moment.duration(end.diff(startTime));
var hours = duration.asHours();
这篇关于在Moment Js中获取两个日期之间的小时差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文