我在 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/