javascript - 添加时间数组的元素

时间:2016-04-25 10:52:28

标签: 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.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);
}

5 个答案:

答案 0 :(得分:2)

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



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>');
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您需要在添加

之前转换为数字
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);

答案 2 :(得分:1)

你在某些地方只会错过一些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);
}

答案 3 :(得分:1)

您可以尝试使用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输出,所以而不是&#39; 30&#39;它会说&#39; 30分钟。

答案 4 :(得分:1)

一个简单的单线功能解决方案可能

&#13;
&#13;
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>");
&#13;
&#13;
&#13;