Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Cómo contar la cantidad de días después de excluir los fines de semana usando js

Tengo una consulta en la que necesito contar el número de días hábiles desde hoy hasta el número dado,

Como hoy es 14/09/2021 y el número dado es 10. Quiero la última fecha 28/09/2021, ya que es el décimo día hábil a partir de hoy. Intenté a continuación contar los días diferentes

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

Estoy recibiendo el 24 de septiembre de 2021. Ahora estoy contando los fines de semana entre dos fechas

 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); }

Y agregar esta cantidad de días en el conteo me da otra fecha, pero estoy confundido si hay nuevamente un fin de semana en el nuevo rango de fechas, entonces, ¿qué puedo hacer con esos días?

Entonces, alguien puede guiarme para esto.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

prueba esto :

 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 Report

0

Otra solución

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!