Ver Mensaje Individual
  #14 (permalink)  
Antiguo 01/12/2002, 18:13
Avatar de Kaopectate
Kaopectate
Colaborador
 
Fecha de Ingreso: diciembre-2001
Ubicación: Curaçao (Antillas Holandesas)
Mensajes: 3.179
Antigüedad: 22 años, 3 meses
Puntos: 38
14.- Resta de horas

P: ¿Como puedo obtener la resta de dos horas almacenadas en elementos TEXT?

R: [ver ejemplo]

Código PHP:
<html>
 <
head>
  <
script language="JavaScript">

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

   function 
stringToSeconds(tiempo){
    var 
sep1 tiempo.indexOf(":");
    var 
sep2 tiempo.lastIndexOf(":");
    var 
hor tiempo.substr(0sep1);
    var 
min tiempo.substr(sep1 1sep2 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(hor2) + ":" padNmb(min2) + ":" padNmb(sec2);
   }

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

   function 
calcT3(){
    
with (document.frm)
     
t3.value substractTimes(t1.valuet2.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> 

Última edición por Kaopectate; 23/12/2002 a las 22:45