[英]How can I do sum two time values in javascript
I've been trying to implement the function that sums two values as hours.我一直在尝试实现将两个值相加为小时的 function。 "Example: 01:30 + 00:30 = 02:00"
“例如:01:30 + 00:30 = 02:00”
So I have this function below that works only if the sum of the two values is equal to a round number such as the example above.所以我在下面有这个 function 只有当两个值的总和等于上面例子的整数时才有效。 But the problem is when the values are say 01:45 + 00:20 it gives me 33:05 instead of 02:05.
但问题是当值是 01:45 + 00:20 时,它给了我 33:05 而不是 02:05。
I've tried several combinations but nothing has worked so far.我尝试了几种组合,但到目前为止没有任何效果。
function sumOFHoursWorked(){
var time1 = "00:45";
var time2 = "01:20";
var hour=0;
var minute=0;
var second=0;
var splitTime1= time1.split(':');
var splitTime2= time2.split(':');
hour = parseInt(splitTime1[0])+parseInt(splitTime2[0]);
minute = parseInt(splitTime1[1])+parseInt(splitTime2[1]);
hour = hour + minute/60;
minute = minute%60;
second = parseInt(splitTime1[2])+parseInt(splitTime2[2]);
minute = minute + second/60;
second = second%60;
var REalhourstime = ('0' + hour).slice(-2)+':'+('0' + minute).slice(-2);
alert(REalhourstime);
document.getElementById('realhorasTB').innerHTML = REalhourstime;
}
I would convert it to minutes and subtract and then calculate hours and minutes.我会将其转换为分钟并减去,然后计算小时和分钟。
function totalMinutes (time) { var parts = time.split(":") return +parts[0] * 60 + +parts[1] } function timeDiff (time1, time2) { var mins1 = totalMinutes(time1) var mins2 = totalMinutes(time2) var diff = mins2 - mins1 var hours = '0' + (Math.floor(diff/60)) var minutes = '0' + (diff - hours * 60) return (hours.slice(-2) + ':' + minutes.slice(-2)) } console.log(timeDiff("00:45", "01:20"))
It will fail for times that go over midnight, a simple less than check can fix that.如果 go 在午夜时分,它会失败,一个简单的小于检查可以解决这个问题。
function totalMinutes (time) { var parts = time.split(":") return +parts[0] * 60 + +parts[1] } function timeDiff (time1, time2) { var mins1 = totalMinutes(time1) var mins2 = totalMinutes(time2) if (mins2 < mins1) { mins2 += 1440 } var diff = mins2 - mins1 var hours = '0' + (Math.floor(diff/60)) var minutes = '0' + (diff - hours * 60) return (hours.slice(-2) + ':' + minutes.slice(-2)) } console.log(timeDiff("23:45", "00:45"))
It actually depends on how your time will be, i mean it will be in mm:ss
formet or hh:mm:ss
or maybe hh:mm:ss:msms
but for just simple second and minutes you can do something like this这实际上取决于您的时间如何,我的意思是它将以
mm:ss
格式或hh:mm:ss
或hh:mm:ss:msms
为单位,但只需简单的几秒钟和几分钟,您就可以做这样的事情
function sumOFHoursWorked(){ var time1 = "00:45".split(':'); var time2 = "01:20".split(':'); let secondSum = Number(time1[1]) + Number(time2[1]); let minSum = Number(time1[0]) + Number(time2[0]); if(secondSum > 59){ secondSum = Math.abs(60 - secondSum); minSum += 1; } if(secondSum < 10){ secondSum = `0${secondSum}`; } if(minSum < 10){ minSum = `0${minSum}`; } return `${minSum}:${secondSum}`; } console.log(sumOFHoursWorked());
time1
and time2
strings are missing the seconds at the end.time1
和time2
字符串最后缺少秒数。 For example, var time1 = "00:45:00"
.var time1 = "00:45:00"
。 Otherwise, your calculation will have some NaN
values.NaN
值。hour
is a floating point number (~ 2.083333333333333), so ('0' + hour)
is '02.083333333333333'.hour
是一个浮点数(~2.083333333333333),所以('0' + hour)
是'02.083333333333333'。 You could use something like this instead: ('0' + Math.floor(hour))
.你可以改用这样的东西:
('0' + Math.floor(hour))
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.