[Vuejs]-Vuejs and laravel calculate hours and minutes

0๐Ÿ‘

โœ…

So this is simple javascript.

If the time string is always going to look the same, you can do:

let times = ["03:30:00", "01:45:00", "00:15:00"]; // you can have an unlimited number of time strings
let hours = 0;
let minutes = 0;
let seconds = 0;
for (const i in times) {
    const time = times[i];
    let splitTime = (time + "").split(":"); // make sure it's a string
    seconds += parseInt(splitTime[2]);
    if(seconds > 59){ // make sure it only goes until 59
        minutes++;
        seconds = seconds % 60;
    }
    minutes += parseInt(splitTime[1]);
    if(minutes > 59){ // make sure it only goes until 59
        hours++;
        minutes = minutes % 60;
    }
    hours += parseInt(splitTime[0]);
}
let totalTime = (hours < 10 ? "0" + hours : hours) + ":"
            + (minutes < 10 ? "0" + minutes : minutes) + ":"
            + (seconds < 10 ? "0" + seconds : seconds); // put the left side zeros

Leave a comment