我有这段代码可以正确找到格式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>
在这种情况下,
t1
和t2
只是两个单独的值要加在一起,然后是一堆这样的数组。两者都应该正常工作。
也请帮助我在此处添加适当的秒数。
答案 0 :(得分:1)
您可以花几秒钟的时间将所有值相加并构建一个新的总和字符串。
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"]]));
答案 1 :(得分:0)
就像提到的评论之一一样,moment
是一个流行的npm软件包,用于处理时间。您可以使用时刻的formatting options来解析数组数据结构中的时间:
const array = [
[0, 0, 9],
[0, 2, 3],
[1, 2, 3],
...
];
const dates = array.map((date) => {
return moment(`${date[0]}-${date[1]}-${date[2]}`, 'hh-mm-ss');
// make sure you account for 12 or 24 hour clocks, not sure which you are using
});
const sum = dates.reduce((date, total) => { return total.add(date); }, moment.duration(0));
答案 2 :(得分:0)
这几乎是相同的数学,只是又花了几秒钟时间->分钟。
seconds = t1[2] + t2[2];
mins = parseInt(seconds / 60);
seconds = seconds % 60;
mins += t1[1] + t2[1];
hrs = parseInt(minutes / 60);
mins = mins % 60;
hrs += t1[0] + t2[0];
我假设您已经知道如何通过简单地使用hh:mm:ss
和split
将Number
转换为适当的数组,但是以防万一:
t1 = time1.split(':').map(part => Number(part));
t2 = time2.split(':').map(part => Number(part));
答案 3 :(得分:0)
您可以使用 reducer 函数汇总相应的数组元素,然后修改结果。
var timeArrays = [
['00', '52', '00'], ['00', '08', '00'], ['00', '00', '40'],
['00', '00', '01'], ['00', '00', '20']
];
function totalTimeAsString(timeArray) {
var result = ['00', '00', '00'];
var reducer = function (acc, current) {
return [
Number(acc[0]) + Number(current[0]),
Number(acc[1]) + Number(current[1]),
Number(acc[2]) + Number(current[2])
];
};
var [hh, mm, ss] = timeArray.reduce(reducer, result);
// seconds
if (ss > 59) {
mm += Math.floor(ss / 60);
}
ss = ss % 60;
result[2] = ((ss < 10) ? '0' : '') + ss;
// minutes
if (mm > 59) {
hh += Math.floor(mm / 60);
}
mm = mm % 60;
result[1] = ((mm < 10) ? '0' : '') + mm;
// hours
result[0] = ((hh < 10) ? '0' : '') + hh;
return result.join(':');
}
console.log(totalTimeAsString(timeArrays));
// "01:01:01"