Foros del Web » Programando para Internet » Javascript »

[SOLUCIONADO] mejorar funcion cuenta horas habiles

Estas en el tema de mejorar funcion cuenta horas habiles en el foro de Javascript en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 16/03/2015, 07:58
 
Fecha de Ingreso: noviembre-2009
Mensajes: 37
Antigüedad: 14 años, 4 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
  #2 (permalink)  
Antiguo 16/03/2015, 08:25
 
Fecha de Ingreso: noviembre-2009
Mensajes: 37
Antigüedad: 14 años, 4 meses
Puntos: 0
Respuesta: mejorar funcion cuenta horas habiles

ya lo solucione:
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.  
  14.      horahabil=9;
  15.      }else{
  16.      
  17.       horahabil=0;
  18.      }
  19.      
  20.       if(hoy.getDay() == 5 && hoy.getDay() != 6 && hoy.getDay() != 0 && (estaarray(fechaformated) )){
  21.    
  22.      horahabilv=8;
  23.      }else{
  24.  
  25.       horahabilv=0;
  26.      }
  27.    
  28.      
  29.     while (hoy<manana) {
  30.       hoy.setTime(hoy.getTime()+24*60*60*1000); // añadimos 1 día
  31.        var fechaformated=hoy.toLocaleFormat('%m/%d');
  32.       if (hoy.getDay() != 6 && hoy.getDay() != 0 && (estaarray(fechaformated))){
  33.         i++;
  34.     }
  35.     if (hoy.getDay() != 5 && hoy.getDay() != 6 && hoy.getDay() != 0 && (estaarray(fechaformated))){
  36.          horahabil+=9;
  37.     }
  38.     if (hoy.getDay() == 5 && hoy.getDay() != 6 && hoy.getDay() != 0 && (estaarray(fechaformated) )){
  39.           horahabilv+=8;
  40.     }
  41.         }
  42.         sumahorahabil=horahabil;
  43.         sumahorahabilv=horahabilv;  
  44.         total=sumahorahabilv+sumahorahabil;
  45.         document.getElementById('diasnomb').value=i;
  46.         document.getElementById('horasnomb').value=total;
  47.      }
Era solo evitar la multiplicacion de las horas por el dia

Etiquetas: funcion, habiles, horas, mejorar, valor
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:00.