Skip to content

Instantly share code, notes, and snippets.

@itsabdelrahman
Created July 23, 2023 13:53
Show Gist options
  • Save itsabdelrahman/4ff8c36f36ee0d89d68e2fc0374d382d to your computer and use it in GitHub Desktop.
Save itsabdelrahman/4ff8c36f36ee0d89d68e2fc0374d382d to your computer and use it in GitHub Desktop.
Get next n-weekdays using date-fns
import { addDays, isWeekend } from "date-fns";
function nextNWeekdays(date, count) {
return nextNWeekdaysHelper(date, count, []);
}
function nextNWeekdaysHelper(date, count, nextWeekdaysSoFar) {
if (count === nextWeekdaysSoFar.length) {
return nextWeekdaysSoFar;
}
const nextDay = addDays(date, 1);
if (isWeekend(nextDay)) {
return nextNWeekdaysHelper(nextDay, count, nextWeekdaysSoFar);
}
return nextNWeekdaysHelper(nextDay, count, [...nextWeekdaysSoFar, nextDay]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment