Skip to content

Commit

Permalink
feat: adds get next week day util
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrk committed Feb 28, 2021
1 parent 60ab774 commit 72a2aad
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/util/getNextDayOfWeek.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { WEEKDAY } from 'constants/global'

const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

/**
* gets the date of the next week day requested
* 0 to 6 or a value of `WEEKDAY`
*/
export function getNextDayOfWeek (day: WEEKDAY | number): Date {
const dayOfWeek = typeof day === 'number' ? day : Math.max(weekdays.indexOf(day), 0)

const date = new Date(Date.now())

date.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7)

return date
}

0 comments on commit 72a2aad

Please sign in to comment.