Created
December 12, 2021 07:12
-
-
Save sererejegede/89be57e02e34b7ece29f9fe29ba883f3 to your computer and use it in GitHub Desktop.
Get any date for the day of the week for passed date.
This file contains hidden or 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
type WeekDays = 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
export function getDayDate(date: Date | string, weekDay: WeekDays) { | |
const _date = new Date(date) | |
const day = _date.getDay() | |
if (day === weekDay) return _date | |
const diff = | |
(day === 0 || day - weekDay === 0 | |
? weekDay - 7 | |
: weekDay === 0 | |
? weekDay + 7 | |
: weekDay) + | |
_date.getDate() - | |
day | |
_date.setDate(diff) | |
return _date | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment