Somar Tempo Horas Minutos e Segundos Javascript

Por Adriano AoliAtualizado em 10/09/2022 09:57

A função abaixo faz a soma de horas minutos e segundos e já retorna formatado.

Ex: var total = somartempos('00:01:01', '00:00:55');

a soma daria 00:01:56

function somartempos(tempo1, tempo2) {

var array1 = tempo1.split(':');

var tempo_seg1 = (parseInt(array1[0]) * 3600) + (parseInt(array1[1]) * 60) + parseInt(array1[2]);

var array2 = tempo2.split(':');

var tempo_seg2 = (parseInt(array2[0]) * 3600) + (parseInt(array2[1]) * 60) + parseInt(array2[2]);

var tempofinal = parseInt(tempo_seg1) + parseInt(tempo_seg2);

var hours = Math.floor(tempofinal / (60 * 60));

var divisorMinutos = tempofinal % (60 * 60);

var minutes = Math.floor(divisorMinutos / 60);

var divisorSeconds = divisorMinutos % 60;

var seconds = Math.ceil(divisorSeconds);

var contador = "";

if (hours < 10) { contador = "0" + hours + ":"; } else { contador = hours + ":"; }

if (minutes < 10) { contador += "0" + minutes + ":"; } else { contador += minutes + ":"; }

if (seconds < 10) { contador += "0" + seconds; } else { contador += seconds; }

return contador;

}

@2006 - 2021 - Adriano AOli

Criação de Sites - Criação de Sistemas

Todos os direitos reservados