简体   繁体   English

如何使用 moment.js 对结果大于 24 小时的日期和时间求和

[英]how to sum dates and times when the result is greater than 24 hours using moment.js

I'm having issues when trying to get the sum of times from an array when the result is over 24 hours.当结果超过 24 小时时,我在尝试从数组中获取时间总和时遇到问题。 I'm using moment.js to get the duration and.reduce() to obtain the total of times, but when the result is over 24 hours: instead of showing 25 hours and 15 minutes, it is showing 1 hour and 15 minutes.我正在使用 moment.js 获取持续时间并使用 .reduce() 获取总次数,但是当结果超过 24 小时时:显示的不是 25 小时 15 分钟,而是 1 小时 15 分钟。

Here is my code:这是我的代码:

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>

Does anyone know how to fix this, so it calculates and shows the right total of hours?有谁知道如何解决这个问题,以便它计算并显示正确的总小时数?

Could you use sum.days() * 24 + sum.hours() for the hours part?小时部分可以使用sum.days() * 24 + sum.hours()吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM