gpt4 book ai didi

javascript - 在时间格式 hh :mm:ss 的数组中查找时间总和

转载 作者:行者123 更新时间:2023-11-30 19:44:43 25 4
gpt4 key购买 nike

我有这段代码可以正确找到 hh:mm 格式的总和我很难将其转换为 hh:mm:ss 格式显然,所有内容都应正确舍入。

这是我希望使用的数组格式,但代码显然仅适用于 ["00", "10"] 作为示例

const array = [
["00", "00", "09"],
["00", "00", "09"],
["00", "00", "04"],
["00", "00", "02"],
["00", "00", "21"],
["00", "00", "14"],
["00", "00", "26"],
["00", "00", "02"],
["00", "00", "14"],
["00", "00", "10"],
["00", "00", "48"],
["00", "00", "12"],
["00", "00", "09"],
["00", "00", "09"]
]

var mins = 0;
var hrs = 0;

if (array == null) {
mins = Number(t1[1]) + Number(t2[1]);
hrs = Math.floor(parseInt(mins / 60));
hrs = Number(t1[0]) + Number(t2[0]) + hrs;
} else {
$.each(array, function(key) {
mins += Number(array[key][1]);
hrs = Math.floor(parseInt(mins / 60));
hrs += Number(array[key][0]) + hrs;
});
}

console.log(mins, hrs)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

t1t2 在这种情况下只是将 2 个单独的值加在一起,然后就有一堆这些值的数组。两者都应该工作正常。

也请帮我在这里正确添加秒数。

最佳答案

您可以获得秒数,将所有值相加并构建一个新的总和字符串。

function sumTime(t1, t2, array = []) {
var times = [3600, 60, 1],
sum = [t1, t2, ...array]
.map(s => s.split(':').reduce((s, v, i) => s + times[i] * v, 0))
.reduce((a, b) => a + b, 0);

return times
.map(t => [Math.floor(sum / t), sum %= t][0])
.map(v => v.toString().padStart(2, 0))
.join(':');
}

console.log(sumTime("01:02:03", "04:57:57"));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));

ES5

function sumTime(t1, t2, array) {
var times = [3600, 60, 1],
sum = [t1, t2].filter(Boolean).concat(array || [])
.map(function (s) {
return s
.split(':') // omit this line for splitted values ['02', '04', '45']
.reduce(function (s, v, i) {
return s + times[i] * v;
}, 0);
})
.reduce(function (a, b) { return a + b; }, 0);

return times
.map(function (t) {
var value = Math.floor(sum / t);
sum %= t;
return value;
})
.map(function (v) {
return v.toString().padStart(2, 0);
})
.join(':');
}


console.log(sumTime("01:02:03", null));
console.log(sumTime(null, "04:57:57"));
console.log(sumTime(null, null, ["01:02:03", "04:57:57"]));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));

使用拆分数组。

function sumTime(t1, t2, array) {
var times = [3600, 60, 1],
sum = [t1, t2].filter(Boolean).concat(array || [])
.map(function (s) {
return s
//.split(':') // omit this line for splitted values ['02', '04', '45']
.reduce(function (s, v, i) {
return s + times[i] * v;
}, 0);
})
.reduce(function (a, b) { return a + b; }, 0);

return times
.map(function (t) {
var value = Math.floor(sum / t);
sum %= t;
return value;
})
.map(function (v) {
return v.toString().padStart(2, 0);
})
.join(':');
}

console.log(sumTime(["01", "02", "03"], null));
console.log(sumTime(null, ["04", "57", "57"]));
console.log(sumTime(null, null, [["01", "02", "03"], ["04", "57", "57"]]));
console.log(sumTime(["00", "00", "09"], ["00", "00", "09"], [["00", "00", "04"], ["00", "00", "02"], ["00", "00", "21"], ["00", "00", "14"], ["00", "00", "26"], ["00", "00", "02"], ["00", "00", "14"], ["00", "00", "10"], ["00", "00", "48"], ["00", "00", "12"], ["00", "00", "09"], ["00", "00", "09"]]));

关于javascript - 在时间格式 hh :mm:ss 的数组中查找时间总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55051019/

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