Ver Mensaje Individual
  #11 (permalink)  
Antiguo 17/10/2012, 08:48
Avatar de JuJoGuAl
JuJoGuAl
 
Fecha de Ingreso: julio-2009
Ubicación: Venezuela
Mensajes: 754
Antigüedad: 14 años, 9 meses
Puntos: 19
Respuesta: Sumar tiempos en un Text Dinamicamente

Listo e Solucionado mi duda posteare el código por si alguien lo necesita:

Con este código no solo sumo las horas si no las redondeo (en caso de sumar 00:50:00 + 00:30:00), ademas agregue una opción personal que si la hora es mayor a 1 no dejarla pasar
Código Javascript:
Ver original
  1. <script>
  2. function calcular(elemento1,elemento2,elemento3,resultado,obj) {
  3.     horas1=document.getElementById(elemento1).value.split(":");
  4.     horas2=document.getElementById(elemento2).value.split(":");
  5.     horas3=document.getElementById(elemento3).value.split(":");
  6.     horatotale=new Array();
  7.     for(a=0;a<3;a++){
  8.         horas1[a]=(isNaN(parseInt(horas1[a])))?0:parseInt(horas1[a])
  9.         horas2[a]=(isNaN(parseInt(horas2[a])))?0:parseInt(horas2[a])
  10.         horas3[a]=(isNaN(parseInt(horas3[a])))?0:parseInt(horas3[a])
  11.         horatotale[a]=(horas1[a]+horas2[a]+horas3[a]);
  12.     }
  13.     tiempototal1=new Date()
  14.     tiempototal1.setHours(horatotale[0]);
  15.     tiempototal1.setMinutes(horatotale[1]);
  16.     tiempototal1.setSeconds(horatotale[2]);
  17.     function completCeros(x,n) {
  18.         x = x.toString();
  19.         while( x.length < n )
  20.             x = "0"+x;
  21.         return x;
  22.     }
  23.     // Creo una variable para capturar los resultados
  24.     var hora=tiempototal1.getHours();
  25.     var minu=tiempototal1.getMinutes();
  26.     var seco=tiempototal1.getSeconds();
  27.     //Completo con '0' si el resultado tiene 1 digito
  28.     hora=completCeros(hora, 2);
  29.     minu=completCeros(minu, 2);
  30.     seco=completCeros(seco, 2);
  31.     //Valido si la hora fue mayor a 1, de ser asi no lo dejo pasar
  32.     if (hora>1)
  33.     {
  34.         alert ('La hora no puede ser mayor a 1 Hora');
  35.         document.getElementById(obj).value='';
  36.         document.getElementById(obj).focus();
  37.     }
  38.     else
  39.     {
  40.         document.getElementById(resultado).value=hora+":"+minu+":"+seco;
  41.     }  
  42. }
  43. </script>

y aqui los campos:
Código HTML:
Ver original
  1. <tr>
  2.             <td align="center"><input type="text" name="tiempotefact1" id="tiempotefact1" onkeypress="return acceptNum(event)" maxlength="10" /></td>
  3. <td align="center"><input type="text" name="TiempoaSumar" id="tiempo11" onblur="calcular('tiempo11','tiempo12','tiempo13','tiempototal1',this.id)" onkeyup="mascara(this,':',patron,true)" maxlength="8" /></td>
  4.             <td align="center"><input type="text" name="TiempoaSumar" id="tiempo12" onblur="calcular('tiempo11','tiempo12','tiempo13','tiempototal1',this.id)" onkeyup="mascara(this,':',patron,true)" maxlength="8" /></td>
  5.             <td align="center"><input type="text" name="TiempoaSumar" id="tiempo13" onblur="calcular('tiempo11','tiempo12','tiempo13','tiempototal1',this.id)" onkeyup="mascara(this,':',patron,true)" maxlength="8" /></td>
  6.             <td align="center"><input name="tiempotetotal1" type="text" id="tiempototal1" readonly="readonly" /></td>
  7.           </tr>

Espero le sirva a alguien mas :)