jquery - 在 HH :MM using jquery 中添加小时数

标签 jquery

我在 jquery 中有多个 HH:MM 格式的时间。喜欢

 t1= '2:20';
 t2= '3:10';
 t3= '2:00';
 t4= '1:00';
 t5= '3:50';
 ............

我们如何获得 HH:MM 格式的所有无限时间的相加。例如,上述时间的总和将为 12:20 。 我已尝试使用以下代码来进行此计算。

    $('.get_sum').each(function() {
            if (($(this).val())) {
                 sum = ($(this).val());
                 ms = new Date('Sep 24 2011 '+sum).getTime(); 
                sum += Number($(this).val());
            }
        });

function msToTime(s) {

  function addZ(n) {
    return (n<10? '0':'') + n;
  }

  var ms = s % 1000;
  s = (s - ms) / 1000;
  var secs = s % 60;
  s = (s - secs) / 60;
  var mins = s % 60;
  var hrs = (s - mins) / 60;

  return addZ(hrs) + ':' + addZ(mins) + ':' + addZ(secs) + '.' + ms;
}

alert(msToTime(sum));

有什么想法吗?

最佳答案

这是分别计算小时和分钟的简单方法..

Fiddle here 这是代码

var totalh =0;
var totalm =0;
$('.get_sum').each(function() {
            if (($(this).val())) {
                var h = parseInt(($(this).val()).split(':')[0]);
                var m = parseInt(($(this).val()).split(':')[1]);
                totalh += h;
                totalm += m;
            }
        });
totalh += Math.floor(totalm / 60);
totalm = totalm % 60;

$('.total').val((totalh < 10 ? '0' : '') + totalh.toString() + ':' + (totalm < 10 ? '0' : '') + totalm.toString())

关于jquery - 在 HH :MM using jquery 中添加小时数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27997269/

相关文章:

javascript - jQuery 小部件 : global variable not consistent

javascript - 如何从自动完成列表中删除以前选择的项目

javascript - 使 CSS 工具提示跟随光标

javascript - JS : function for every ul in variable

c# - JQuery ajax 调用 httpget webmethod (c#) 不工作

javascript - 需要做一个垂直的时间轴

javascript - 如果元素宽度有效,则使用 jQuery 函数

javascript - 表单未按时自动提交

javascript - jQuery 插件功能单击时更改背景颜色

javascript - 向 PHP 发送 Ajax 请求不起作用