Calculate the sum of the time for column

Total duration time
Anna 31800:00:50 62700
Bob 318 00:00:27 62703
Mike 318 00:00:36 88455233284
var totalDurationTime = document.getElementsByClassName("total_duration_time")[0],
    durationTimes = document.getElementsByClassName("duration_time"),
    total = {
        hours: 0,
        minutes: 0,
        seconds: 0
    };

function zeroPad (num) {
    var str = num.toString();
    
    if (str.length < 2) {
        str = "0" + str;
    }
    
    return str;
}

Array.prototype.forEach.call(durationTimes, function (time) {
    var parts = time.textContent.split(":"),
        temp;
    
    temp = (+parts[2] + total.seconds);
    total.seconds = temp % 60;    
    temp = (+parts[1] + total.minutes) + (temp - total.seconds) / 60;
    total.minutes = temp % 60;    
    total.hours = (+parts[0] + total.hours) + (temp - total.minutes) / 60;
});

totalDurationTime.textContent = zeroPad(total.hours) + ":" + zeroPad(total.minutes) + ":" + zeroPad(total.seconds);

No comments:

What is Security Engineering? Part 2.

A broad overview of the ever-developing security engineering field; a domain that can feel intimidating to some software engineers. With Nie...

Contact Form

Name

Email *

Message *