添加时间数组的元素

adding elements of time array

本文关键字:元素 数组 时间 添加      更新时间:2023-09-26

我有一个由像['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>");