Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/03/2015, 07:58
lavos
 
Fecha de Ingreso: noviembre-2009
Mensajes: 37
Antigüedad: 14 años, 5 meses
Puntos: 0
mejorar funcion cuenta horas habiles

Saludos, tengo un codigo que copie de una pregunta en este foro para contar dias habiles(L-V sin feriados) la cual funciona bien, pero intente modificarla para que me cuente las horas habiles que estan entre la fecha de inicio y la de final tomando en cuenta que de lunes a jueves se trabajan 9 horas y los viernes 8 horas, si la fecha de inicio es desde un lunes a un viernes no hay problema pero si hago que la fecha de inicio sea de un viernes y la de final un martes no cuenta las horas.
Código Javascript:
Ver original
  1. function calcularnombramiento(){
  2. var inicinomb=document.getElementById('nombdesde').value;
  3. var hoy = new Date(inicinomb);
  4. var finnomb=document.getElementById('nombhasta').value;
  5. var manana = new Date(finnomb);
  6.  var fechaformated=hoy.toLocaleFormat('%m/%d');
  7. if(hoy.getDay() != 6 && hoy.getDay() != 0 && (estaarray(fechaformated) )){
  8.   i=1;    
  9.  }else{
  10.   i=0;  
  11.  }    
  12. if(hoy.getDay() != 5 && hoy.getDay() != 6 && hoy.getDay() != 0 && estaarray(fechaformated) )){
  13.  b=1;  
  14.  horahabil=9;
  15.  }else{
  16.   b=0;
  17.   horahabil=0;
  18.  }
  19.  
  20.  // if(hoy.getDay() == 5 && hoy.getDay() != 6 && hoy.getDay() != 0 && (fechaformated!=juevessanto && fechaformated!=viernessanto &&
  21. //fechaformated!=enero && fechaformated!=trabajador && fechaformated!=julio && fechaformated!=madres && fechaformated!=indepen
  22. //&& fechaformated!=raza && fechaformated!=navidad )){
  23.  a=0;  
  24.  horahabilv=8;
  25. // }else{
  26. //  a=1;
  27. //  horahabilv=0;
  28. // }
  29. //a=0;
  30. //horahabilv=8;
  31.  
  32. while (hoy<manana) {
  33.   hoy.setTime(hoy.getTime()+24*60*60*1000); // añadimos 1 día
  34.    var fechaformated=hoy.toLocaleFormat('%m/%d');
  35.   if (hoy.getDay() != 6 && hoy.getDay() != 0 && (estaarray(fechaformated))){
  36.     i++;
  37. }
  38. if (hoy.getDay() != 5 && hoy.getDay() != 6 && hoy.getDay() != 0 && (estaarray(fechaformated))){
  39.     b++;
  40. }
  41. if (hoy.getDay() == 5 && hoy.getDay() != 6 && hoy.getDay() != 0 && (estaarray(fechaformated) )){
  42.     a++;
  43. }
  44.     }
  45.     sumahorahabil=horahabil*b;
  46.     sumahorahabilv=horahabilv*a;  
  47.     total=sumahorahabilv+sumahorahabil;
  48.     document.getElementById('diasnomb').value=i;
  49.     document.getElementById('horasnomb').value=total;
  50.  }
Trate de validar si el inicio de la fecha es un dia de lunes a jueves su contador inicie en 0 o 1 y el valor de las horas para ese dia en 9 o 0 igual para el viernes, estaaaray es una funcion con los dias de feriado.

Última edición por lavos; 16/03/2015 a las 08:08