Created
July 23, 2023 13:53
-
-
Save itsabdelrahman/4ff8c36f36ee0d89d68e2fc0374d382d to your computer and use it in GitHub Desktop.
Get next n-weekdays using date-fns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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