Menu

gitpiper

addWeekDays javascript Code Snippet in 2024

dateintermediate

Last Updated: 9 July 2024

Calculates the date after adding the given number of business days.

  • Use Array.from() to construct an array with length equal to the count of business days to be added.
  • Use Array.prototype.reduce() to iterate over the array, starting from startDate and incrementing, using Date.prototype.getDate() and Date.prototype.setDate().
  • If the current date is on a weekend, update it again by adding either one day or two days to make it a weekday.
  • NOTE: Does not take official holidays into account.
const addWeekDays = (startDate, count) => Array.from({ length: count }).reduce(date => { date = new Date(date.setDate(date.getDate() + 1)); if (date.getDay() % 6 === 0) date = new Date(date.setDate(date.getDate() + (date.getDay() / 6 + 1))); return date; }, startDate);
addWeekDays(new Date('Oct 09, 2020'), 5); // 'Oct 16, 2020' addWeekDays(new Date('Oct 12, 2020'), 5); // 'Oct 19, 2020'

javascript snippet similar to addWeekDays For You in July 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! ✌️