adding two times in javascript

Here I will show you that how can we add two times of 24 hour hh:mm:ss format in javascript

For this we need times in hh:mm:ss format (to see how to convrt time in hh:mm:ss format refer my Converting time from seconds into HH:MM:SS format in javascript tutorial)

Here is code

String.prototype.toHHMMSS = function () {
    var sec_num = parseInt(this, 10); // don't forget the second parm
    var hours = Math.floor(sec_num / 3600);
  //  alert(hours);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours < 10) { hours = "0" + hours; }
    if (minutes < 10) { minutes = "0" + minutes; }
    if (seconds < 10) { seconds = "0" + seconds; }
    var time = hours + ':' + minutes + ':' + seconds;
    return time;
}

function dateAdd(time1,time2) {
    var part = time1.split(":");
    var t1Millisec = (parseInt(part[0]) * 3600000 ) + (parseInt(part[1] * 60000) + (parseInt(part[2] * 1000)));
    
    var part = time2.split(":");
    var t2Millisec = (parseInt(part[0]) * 3600000) + (parseInt(part[1] * 60000) + (parseInt(part[2] * 1000)));

    return (parseInt(Math.abs(t1Millisec + t2Millisec) / 1000)).toString().toHHMMSS();
}

admin