添加时间数组的元素
adding elements of time array
我有一个由像['00:30', '01:30', '03:00', '04:30']
这样的持续时间组成的数组,我正在尝试添加数组元素以获得总持续时间。我正在尝试以下代码来完成这项工作,但我得到了一个不相关的输出,例如 00000.5010.503040.5:0
.以前有人尝试过这种金额吗?
function calc_tot_dur() {
var total_durs = ['00:30', '01:30', '03:00', '04:30'];
var dim = '00:00';
jQuery.each(total_durs, function(index, value) {
//console.log(value);
dim_split = dim.split(":");
hs = dim_split[0];
ms = dim_split[1];
value_split = value.split(":");
v_hs = value_split[0];
v_ms = value_split[1];
console.log(hs + v_hs);
dim = (hs + v_hs) + ':' + (ms + v_ms);
// console.log(dim);
ms_hs = (ms + v_ms) / 60;
if (ms_hs > 0) {
dim = (hs + v_hs + ms_hs) + ':' + (00);
} else {
dim = (hs + v_hs) + ':' + (ms + v_ms);
}
});
alert(dim);
}
对零件长度进行缩减和校正的解决方案。
var total_durs = ['00:30', '01:30', '03:00', '04:30'],
result = function (array) {
var total = array.reduce(function (r, a) {
var aa = a.split(':').map(Number);
return r + 60 * aa[0] + aa[1];
}, 0);
return [Math.floor(total / 60), total % 60].map(function (a) {
var s = a.toString();
return s.length < 2 ? '0' + s : s;
}).join(':');
}(total_durs);
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
你只在某些地方错过了一些parseInt,所以当你求和时
"00" + "00" //as string it become 0000
parseInt("00") + parseInt("00") //will become 0
所以变化:
function calc_tot_dur() {
var total_durs = ['00:30', '01:30', '03:00', '04:30'];
var dim = '00:00';
jQuery.each(total_durs, function(index, value) {
dim_split = dim.split(":");
hs = parseInt(dim_split[0]); // here
ms = parseInt(dim_split[1]); // here
value_split = value.split(":");
v_hs = parseInt(value_split[0]); // here
v_ms = parseInt(value_split[1]); // here
dim = (hs + v_hs) + ':' + (ms + v_ms);
ms_hs = parseInt((ms + v_ms) / 60); // here
if (ms_hs > 0) {
dim = (hs + v_hs + ms_hs) + ':' + (00);
} else {
dim = (hs + v_hs) + ':' + (ms + v_ms);
}
});
console.log(dim);
}
你可以尝试使用Moment,这是一个在javascript中处理日期和时间持续时间的好库。 这是一个小提琴,展示了它的实际效果:
https://jsfiddle.net/25zfe4h1/
function calc_tot_dur() {
var total_durs = ['00:30', '01:30', '03:00', '04:30'];
var dim = '00:00';
jQuery.each(total_durs, function(index, value) {
var duration = moment.duration(value);
dim = moment.duration(dim).add(duration);
});
alert(formatResult(dim));
}
function formatResult(res) {
return res.hours() + ':' + res.minutes();
// return res.humanize();
}
calc_tot_dur();
注意到你必须少写多少吗? 它还具有您可以humanize
输出的好处,因此可以说"30 分钟"而不是"00:30"。
您需要在
添加之前转换为数字
var total_durs = ['00:30', '01:30', '03:00', '04:30'];
var total = 0;
total_durs.forEach(function(val){
var times = val.split(":");
var num = parseInt(times[0],10) + parseInt(times[1],10)/60;
total += num;
});
现在将total
转换为时间格式(hh:mm)
var numstring = String(total).split(".");
var display = ("0" + String(numstring[0])).slice(-2) + ":" + String(numstring[1]*6);
演示
var total_durs = ['00:30', '01:30', '03:00', '04:30'];
var total = 0;
total_durs.forEach(function(val){
var times = val.split(":");
var num = parseInt(times[0],10) + parseInt(times[1],10)/60;
total += num;
});
var numstring = String(total).split(".");
var display = ("0" + String(numstring[0])).slice(-2) + ":" + String(numstring[1]*6);
alert(display);
一个简单的单衬功能解决方案可以是
var arr = ['00:30', '01:30', '03:00', '04:30'],
tot = arr.map(e => e.split(":").map(e => e*1)).reduce((p,c) => [p[0] + c[0] + ~~((p[1]+c[1])/60), (p[1]+c[1])%60]);
document.write("<pre>" + tot[0] + ":" + tot[1] + "</pre>");
相关文章:
- 在函数中添加数组元素的数值
- 访问JSON对象内部的数组元素
- 用于检查数组中是否存在元素的javascript自定义方法
- 在数组中的一个元素上设置多个值
- 从组件状态的数组中删除元素
- Mongoose-在更新中删除数组元素
- 从数组中删除元素的最佳方法是:javascript/jquery
- 数组中元素(字符串)的总和
- javascript数组元素是否知道其封闭数组
- 查找数组javascript中包含的元素类型
- 如何在javascript上向数组的对象添加新元素
- 如果数组仍在执行,async.forEach()将运行我添加到数组中的新元素
- 使用filter和map方法将数组中某些元素的第一个字母大写-JavaScript
- 一些元素没有从数组中删除
- JS.循环遍历多维数组,以计数元素在每列中的出现次数
- 将数组元素附加到FormData dos'不适用于Firefox 15
- 元素不会添加到Javascript数组中
- 如何从另一个带下划线的数组中筛选带元素的数组
- 如何将函数应用于两个数组的第 n 个元素(数组的数组)
- Js接受两个值之间的元素(数组)