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

206
Views
¿Cómo encontrar el número de días de los días dados en js?

Necesito encontrar el número de días si ingreso la fecha de inicio y la fecha de finalización. Desde la fecha de inicio hasta la fecha de finalización, solo quiero recuperar los días de la semana, es decir, de lunes a viernes, junto con la variable offcial_leave for_example:

 let numberOfdays; let startDate = '2022-04-04'; //yy-mm-dd format let endDate = '2022-04-08'; // Below variable come from db and vary according the start and endate // eg:- 2022-12-25 will be holiday if we select start and end in december let holidays = ['2022-04-05', '2022-04-07' ] numberOfdays => 3 // I want to return number of days to 3

¿Cómo puedo lograr esto en JavaScript?

Gracias

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

0

Primero convierta startDate y endDate a javascript Date . Luego, declare una variable i para almacenar mientras recorre la fecha. Además, declare holidayIndex que almacena el índice actual en el que la fecha de vacaciones debe verificarse con la fecha actual.

Dentro del bucle, convierta la fecha al YYYY-MM-DD (formato original) para comprobar si la fecha actual ( isoDate ) se encuentra entre un día festivo, es decir, no es una fecha de día festivo. Si el índice de vacaciones está en el último índice de la matriz, simplemente verifique si la fecha actual ( isoDate ) no está en la matriz de vacaciones. Si no se encuentra, incremente la variable numberOfDays .

De lo contrario, se encuentra una fecha de vacaciones, por lo que no es necesario incrementar numberOfDays . Simplemente incremente el holidayIndex de vacaciones para estar listo para coincidir con las próximas fechas para la próxima fecha de vacaciones.

Aquí está la solución:

 let numberOfdays = 0; const startDate = '2022-04-04'; //yy-mm-dd format const endDate = '2022-04-08'; // Below variable come from db and vary according the start and endate // eg:- 2022-12-25 will be holiday if we select start and end in december const holidays = ['2022-04-05', '2022-04-07']; let holidayIndex = 0; const start = new Date(startDate); const end = new Date(endDate); let i = start; while (i <= end) { const isoDate = i.toISOString().split('T')[0]; if ( (holidayIndex < holidays.length - 1 && isoDate < holidays[holidayIndex] && isoDate > holidays[holidayIndex + 1]) || formattedDate !== holidays[holidayIndex] ) { numberOfdays += 1; } else { holidayIndex += 1; } i.setDate(i.getDate() + 1); }
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!