gpt4 book ai didi

javascript - 在 Javascript 中将持续时间加在一起

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:43:40 24 4
gpt4 key购买 nike

给定一个时间数组:

["01:08", "03:46", "03:24", "05:53", "01:45", "03:32", "05:19", "08:56", "01:49", "05:40", "05:21", "02:40", "04:26", "02:02", "04:42", "03:58", "02:06", "02:46", "05:21", "03:37", "02:55", "03:26", "04:16", "01:32", "01:42", "03:22", "01:55", "01:41", "05:10", "00:45", "03:23", "05:08", "02:22", "02:34", "02:49", "01:18", "02:13", "01:37", "03:36", "05:26", "05:00", "02:41", "03:08", "01:00", "02:19", "02:33", "03:43", "01:35", "02:59", "01:38", "04:05", "04:15", "03:43", "03:43", "00:25"]

其中每个时间都是分钟和秒的持续时间。

如何将这些持续时间相加以获得最终持续时间?

我尝试使用日期对象,但我似乎无法理解。

最佳答案

对于更函数式的样式,可以使用map将每个时间转换为秒格式,然后使用reduce获取所有时间的总和。

function toSeconds(time) {
var minutes = Number(time.slice(0, 2));
var seconds = Number(time.slice(3));
return seconds + minutes * 60;
}

function sum(a, b) {
return a + b;
}

// Assuming your array is named 'arr'
var totalSeconds = arr.map(toSeconds).reduce(sum);
console.log(totalSeconds);

关于javascript - 在 Javascript 中将持续时间加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34342630/

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