Menu

gitpiper

countWeekDaysBetween javascript Code Snippet in 2024

dateintermediate

Last Updated: 17 December 2024

Counts the weekdays between two dates.

  • Use Array.from() to construct an array with length equal to the number of days between startDate and endDate.
  • Use Array.prototype.reduce() to iterate over the array, checking if each date is a weekday and incrementing count.
  • Update startDate with the next day each loop using Date.prototype.getDate() and Date.prototype.setDate() to advance it by one day.
  • NOTE: Does not take official holidays into account.
const countWeekDaysBetween = (startDate, endDate) => Array .from({ length: (endDate - startDate) / (1000 * 3600 * 24) }) .reduce(count => { if (startDate.getDay() % 6 !== 0) count++; startDate = new Date(startDate.setDate(startDate.getDate() + 1)); return count; }, 0);
countWeekDaysBetween(new Date('Oct 05, 2020'), new Date('Oct 06, 2020')); // 1 countWeekDaysBetween(new Date('Oct 05, 2020'), new Date('Oct 14, 2020')); // 7

javascript snippet similar to countWeekDaysBetween For You in December 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️