当结果超过24小时时,尝试从数组中获取时间总和时遇到问题。我正在使用moment.js来获取持续时间,并使用.reduce()来获取总时间,但是当结果超过24小时时:而不是显示25小时15分钟,而是显示1小时15分钟。
这是我的代码:
const arrayOfTimes = ["02:24", "01:30", "20:00", "01:17", "00:45"] //25 hours and 56 minutes in total
const sum = arrayOfTimes.reduce(
(time1, time2) => time1.add(moment.duration(time2)),
moment.duration()
);
JSX
<h4 className="ml-3 text-light text-center">
Total time worked:
{Math.floor(sum.hours()) +
" hours and " + sum.minutes() +
" minutes"} //here is where it's showing 1 hour and 56 minutes instead of 25 hours and 56 minutes
</h4>
有人知道如何解决此问题,以便它计算并显示正确的总工作时间吗?
您可以sum.days() * 24 + sum.hours()
在小时部分使用吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句