Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript-添加时间数组元素_Javascript_Arrays_Time - Fatal编程技术网

javascript-添加时间数组元素

javascript-添加时间数组元素,javascript,arrays,time,Javascript,Arrays,Time,我有一个由持续时间组成的数组,如['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.

我有一个由持续时间组成的数组,如
['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'];
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 total_durs=['00:30','01:30','03:00','04:30'];
var合计=0;
每小时总费用(功能(val){
var times=val.split(“:”);
var num=parseInt(次[0],10)+parseInt(次[1],10)/60;
总数+=num;
});
var numstring=String(总计).split(“.”);
变量显示=(“0”+字符串(numstring[0]))。切片(-2)+:“+字符串(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;
});
现在将
total
转换为时间格式(hh:mm)

演示

var total_durs=['00:30','01:30','03:00','04:30'];
var合计=0;
每小时总费用(功能(val){
var times=val.split(“:”);
var num=parseInt(次[0],10)+parseInt(次[1],10)/60;
总数+=num;
});
var numstring=String(总计).split(“.”);
变量显示=(“0”+字符串(numstring[0]))。切片(-2)+:“+字符串(numstring[1]*6);

警报(显示)在某些地方,您只会错过一些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);
}

您只在某些地方遗漏了一些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);
}
您可以尝试使用,这是一个在javascript中处理日期和持续时间的很好的库。下面是一把小提琴,它在演奏:

注意到你要写的东西少了多少?它还有一个好处,就是您可以
使输出人性化,因此它会说“30分钟”,而不是“00:30”。

您可以尝试使用,这是一个用javascript处理日期和持续时间的很好的库。下面是一把小提琴,它在演奏:


注意到你要写的东西少了多少?它还有一个好处,那就是你可以将输出人性化,因此它会说“30分钟”,而不是“00:30”。

一个减少和校正零件长度的解决方案

var total_durs=['00:30','01:30','03:00','04:30'],
结果=函数(数组){
var total=array.reduce(函数(r,a){
var aa=a.split(“:”).map(编号);
返回r+60*aa[0]+aa[1];
}, 0);
返回[Math.floor(总计/60),总计%60]。映射(函数(a){
var s=a.toString();
返回s.length<2?'0'+s:s;
})。加入(“:”);
}(总计);

document.write(“”+JSON.stringify(结果,0,4)+“”)减少零件长度并对其进行校正的解决方案

var total_durs=['00:30','01:30','03:00','04:30'],
结果=函数(数组){
var total=array.reduce(函数(r,a){
var aa=a.split(“:”).map(编号);
返回r+60*aa[0]+aa[1];
}, 0);
返回[Math.floor(总计/60),总计%60]。映射(函数(a){
var s=a.toString();
返回s.length<2?'0'+s:s;
})。加入(“:”);
}(总计);

document.write(“”+JSON.stringify(结果,0,4)+“”)一个简单的单线性函数解决方案可以是

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);

写(“+tot[0]+”:“+tot[1]+”)一个简单的单线性函数解决方案可以是

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);

写(“+tot[0]+”:“+tot[1]+”)
您正在添加字符串,而不是数字Yes,最重要的是,您正在为循环的每次迭代显示和修改
dim
变量。添加字符串将导致将它们连接起来。您需要将它们转换为数字才能添加它们您正在添加字符串,而不是数字是的,最重要的是,您正在为循环的每次迭代显示和修改
dim
变量。添加字符串会将它们连接起来。您需要将它们转换为数字以添加它们竖起竖起以校正零件长度竖起以校正零件长度