Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
How to count number of days after excluding weekends using js

I have one query where I need to count number of business days from today to given number,

Like today is 09/14/2021 & given number is 10 I want the last date 09/28/2021 as it is 10th working day from today. I tried below to count day different

    var startDate = new Date();
    var endDate = new Date();
    if(res.defaultLeadTime !== 0){
        endDate.setDate(startDate.getDate() + 10);
    }

I am getting 24th Sep, 2021. Now I am counting weekends between two dates

countWeekendDays = ( d0, d1 ) => {
    var ndays = 1 + Math.round((d1.getTime()-d0.getTime())/(24*3600*1000));
    var nsaturdays = Math.floor( (d0.getDay()+ndays) / 7 );
    return 2*nsaturdays + (d0.getDay()==0) - (d1.getDay()==6);
}

And adding these number of days in the counting gives me another date but I am confuse if there is again weekend in the new date range then what can I do with that days.

So anyone can please guide me for this.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

try this :

var startDate = "14-SEPT-2021";
startDate = new Date(startDate.replace(/-/g, "/"));
var endDate = "", noOfDaysToAdd = 10, count = 0;
while(count < noOfDaysToAdd){
    endDate = new Date(startDate.setDate(startDate.getDate() + 1));
    if(endDate.getDay() != 0 && endDate.getDay() != 6){
       count++;
    }
}
alert(endDate);//You can format this date as per your requirement

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another Solution

startDate = new Date("09/17/2021");  
endDate = new Date("10/28/2021");  

 
// Calculate days_difference = (endDate milliseconds - startDate milliseconds) / (1000 * 60 * 60 * 24) to get results in days  
var days_difference = (endDate.getTime() - startDate.getTime()) / 86400000; 
 
// Calculate number of weekends during the whole duration
var weekdends = Math.floor(days_difference/7) * 2 ;

// Adjust weekends depend on start days 
(days_difference%7)+startDate.getDay() == 6 ? weekdends+=2 : 
  (days_difference%7)+startDate.getDay() == 5 ? weekdends+=1 :
  weekdends;

// Calculate Working Days 
var workDays = days_difference - weekdends

console.log("Working days",(workDays));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda