ago 07
A função abaixo tem o objetivo de subtrair duas horas passadas para ela.
-
function subtraiHora(hrA, hrB) {
-
if(hrA.length != 5 || hrB.length != 5) return "00:00";
-
-
temp = 0;
-
nova_h = 0;
-
novo_m = 0;
-
-
hora1 = hrA.substr(0, 2) * 1;
-
hora2 = hrB.substr(0, 2) * 1;
-
minu1 = hrA.substr(3, 2) * 1;
-
minu2 = hrB.substr(3, 2) * 1;
-
-
temp = minu1 – minu2;
-
while(temp < 0) {
-
nova_h++;
-
temp = temp + 60;
-
}
-
novo_m = temp.toString().length == 2 ? temp : ("0" + temp);
-
-
temp = hora1 – hora2 – nova_h;
-
while(temp < 0) {
-
temp = temp + 24;
-
}
-
nova_h = temp.toString().length == 2 ? temp : ("0" + temp);
-
-
return nova_h + ‘:’ + novo_m;
-
}
Como usar:
-
novaHora = subtraiHora("12:00", "02:27");
-
novaHora -> "09:33"

Recent Comments