gpt4 book ai didi

momentjs - 获取 hh :mm between two datetimes using moment 的时差

转载 作者:行者123 更新时间:2023-12-04 23:15:47 25 4
gpt4 key购买 nike

我在获取两个时间戳(纪元格式)之间的时差时遇到问题。

我用过 moment().format("L LT")将其转换为所需的日期时间格式( MM/DD/YYYY HH:MM (12 hour) ),
但现在的问题是,我想要 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

我不知道这里发生了什么问题。

最佳答案

根据之前给出的答案,我能够解决相同的逻辑并为我的问题获得正确的解决方案。

let start moment(startTimeStampInMS),
end = moment(endTimeStampInMS);

let timediffms = end-start; // difference in ms
let duration = moment.duration(timediffms);
// _data should not be used because it is an internal property and can
//change anytime without any warning
//let hours = duration._data.days*24 + duration._data.hours;
//let min = duration._data.minutes;
let hours = duration.days()*24+ duration.hours();
let min = duration.minutes();
timeDifference = hours +":"+ min; // Answer

关于momentjs - 获取 hh :mm between two datetimes using moment 的时差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42968462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com