简体   繁体   中英

How can I do sum two time values in javascript

I've been trying to implement the function that sums two values as hours. "Example: 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. But the problem is when the values are say 01:45 + 00:20 it gives me 33:05 instead of 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.

 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

 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());

  1. First of all, the time1 and time2 strings are missing the seconds at the end. For example, var time1 = "00:45:00" . Otherwise, your calculation will have some NaN values.
  2. The main issue is that hour is a floating point number (~ 2.083333333333333), so ('0' + hour) is '02.083333333333333'.

You could use something like this instead: ('0' + Math.floor(hour)) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM