Ver Mensaje Individual
  #3 (permalink)  
Antiguo 05/02/2007, 12:37
carolina
 
Fecha de Ingreso: enero-2007
Mensajes: 158
Antigüedad: 17 años, 3 meses
Puntos: 0
Re: formato de hora hh:mm:ss

O alguien sabe como con el codigo de mas abaja poder hacer que la funcion calcule solo horas y minutos? debido a que esta funciona de marabilla si le pasamos dos text en formato hh:mm:ss pero si le pasamos uno de los text en formato hh:mm queda la escoba y entrega valores negativos



<html>
<head>
<script language="JavaScript">

function padNmb(nStr, nLen){
var sRes = String(nStr);
var sCeros = "0000000000";
return sCeros.substr(0, nLen - sRes.length) + sRes;
}

function stringToSeconds(tiempo){
var sep1 = tiempo.indexOf(":");
var sep2 = tiempo.lastIndexOf(":");
var hor = tiempo.substr(0, sep1);
var min = tiempo.substr(sep1 + 1, sep2 - sep1 - 1);
var sec = tiempo.substr(sep2 + 1);
return (Number(sec) + (Number(min) * 60) + (Number(hor) * 3600));
}

function secondsToTime(secs){
var hor = Math.floor(secs / 3600);
var min = Math.floor((secs - (hor * 3600)) / 60);
var sec = secs - (hor * 3600) - (min * 60);
return padNmb(hor, 2) + ":" + padNmb(min, 2) + ":" + padNmb(sec, 2);
}

function substractTimes(t1, t2){
var secs1 = stringToSeconds(t1);
var secs2 = stringToSeconds(t2);
var secsDif = secs1 - secs2;
return secondsToTime(secsDif);
}

function calcT3(){
with (document.frm)
t3.value = substractTimes(t1.value, t2.value);
}

</script>
</head>
<body>
<form name="frm">
Hora1 (hh:mm:ss): <input type="text" name="t1" value="12:30:15"><br>
Hora2 (hh:mm:ss): <input type="text" name="t2" value="3:40:18"><br>
<hr>
Resta (hh:mm:ss): <input type="text" name="t3" value=""><br><br>
<input type="button" onclick="calcT3()" value="Restar">
</form>
</body>
</html>