获取 Moment Js 中两个日期之间的时差 [英] Get hours difference between two dates in Moment Js

查看:41
本文介绍了获取 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() 方法(参见 the文档).

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屋!

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