934 B
934 B
title, tags
| title | tags |
|---|---|
| addWeekDays | array,date,intermediate |
Returns a date after adding given number of business days.
- Use
Array.from()to construct an array withlengthequal to thebusinessDayCount. - Use
Array.prototype.reduce()to iterate over the array, and incrementstartDateusingDate.getDate()andDate.setDate(). - If
startDatefalls on a weekend, update it again by adding either one day or two days to make it a weekday.
const addWeekDays = (startDate, businessDayCount) =>
Array
.from({ length: businessDayCount })
.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'