给出一系列时间:
["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"]
每次都是分钟和秒的持续时间。
如何将这些持续时间加在一起以获得最终持续时间?
我尝试使用日期对象,但我似乎无法弄明白。
答案 0 :(得分:3)
对于更实用的样式,您可以使用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);
答案 1 :(得分:2)
还有一个:
var t = ["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"];
var seconds = t.map(function(v) {
var ary = v.split(':');
return +ary[0] + ary[1] / 60;
}).reduce(function(p, c) {
return p + c;
});
答案 2 :(得分:1)
循环遍历数组时,保持一个总计秒(seconds + (minutes * 60)
)。最后从秒转换为MM:SS
格式。
var data = ["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"];
var durationSum = function(data) {
var totalSeconds = 0;
for (var i = 0; i < data.length; i++) {
var split = data.split(':');
var minutes = parseInt(split[0]);
var seconds = parseInt(split[1]);
totalSeconds = seconds + (minutes * 60);
}
var minutes = Math.floor(totalSeconds / 60);
var seconds = totalSeconds - minutes;
minutes = minutes.toString();
seconds = seconds.toString();
while (minutes.length < 2) minutes = '0' + minutes;
while (seconds.length < 2) seconds = '0' + seconds;
return minutes + ':' + seconds;
}
console.log(durationSum(data));
答案 3 :(得分:0)
var da = ["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"];
var mins = 0;
var secs = 0;
for(var i=0; i<da.length; i++) {
mins = mins + Number(da[i].substr(0, 2));
secs = secs + Number(da[i].substr(3, 2));
}
mins = mins + Math.floor(secs / 60);
secs = secs % 60;
console.log(mins + ":" + secs);
答案 4 :(得分:0)
将每个条目拆分为分钟和秒,并计算十进制分钟数。添加它们,然后转换回分钟和秒钟。
这里有一个小实用功能来总结一个数组:
Eq a => a -> [a] -> Int
编写另一个小函数来分割时间条目并返回十进制小数分钟。我们将在function add(a, b) { return a + b; }
function sum(a) { return a.reduce(add); }
下面使用此功能:
map
使用sum函数将使用function split(x) {
var parts = elt.split(':');
return +parts[0] + parts[1]/60;
}
将输入数组的每个元素映射到十进制分钟数的结果总和:
split
现在您可以打印出结果的分钟数和秒数:
var minutes = sum(array.map(split));
答案 5 :(得分:0)
这个可爱的小代码是什么?
function sumUp(x){
var min = 0;
[].forEach.call(x, function(inst){
var _x = inst.split(":");
min += +(_x[0])*60 + +(_x[1]);
});
return Math.floor(min/60)+":"+(min%60);
}
此处x
是一个时间数组。