moment获取几小时前_momentjs – 使用时刻在两个日期时间之间获得hh:mm的时差

在JavaScript中,遇到计算两个epoch时间戳之间的时间差异问题,转换为MM/DD/YYYYHH:mm格式后,时间差异计算不正确,尤其当时间跨天时。已尝试使用moment.js库进行格式化和计算,但结果有时错误。例如,'03/20/2017 3:11PM'到'03/21/2017 9:45AM'的时间差显示为30:24,而应为22:34。
摘要由CSDN通过智能技术生成

我在获取两个时间戳(epoch格式)之间的时间差异方面存在问题.

我使用moment().format(“L LT”)将其转换为所需的日期时间格式(MM / DD / YYYY HH:MM(12小时)),

但现在的问题是,我希望hh:mm之间的时间差异

P.S.:它也可能超过24小时.

我尝试了什么:

let timeDiffms,duration,timedifference,

start moment(startTimeStampInMS).format("L LT"),

end = moment(endTimeStampInMS).format("L LT");

timeDiffms = moment(end, "MM/DD/YYYY HH:mm").diff(moment(start, "MM/DD/YYYY HH:mm"));

duration = moment.duration(timeDiffms);

timedifference = Math.floor(duration.asHours()) +":"+duration.minutes(); // result

//output I am getting

// start = "03/20/2017 3:11 PM" end="03/21/2017 9:45 AM" timedifference = "30:24", (incorrect)

// start = "03/20/2017 10:07 AM" end="03/23/2017 11:24 AM" timedifference = "73:17" (correct)

// start = "03/20/2017 3:11 PM" end="03/23/2017 11:31 AM" timedifference = "80:20" (incorrect) strange

我不知道这里发生了什么错.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值