This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
frontend/src/helpers/time/calculateDayInterval.ts

27 lines
683 B
TypeScript

export function calculateDayInterval(date, currentDay = (new Date().getDay())): number {
switch (date) {
case 'today':
return 0
case 'tomorrow':
return 1
case 'nextMonday':
// Monday is 1, so we calculate the distance to the next 1
return (currentDay + (8 - currentDay * 2)) % 7
case 'thisWeekend':
// Saturday is 6 so we calculate the distance to the next 6
return (6 - currentDay) % 6
case 'laterThisWeek':
if (currentDay === 5 || currentDay === 6 || currentDay === 0) {
return 0
}
return 2
case 'laterNextWeek':
return calculateDayInterval('laterThisWeek', currentDay) + 7
case 'nextWeek':
return 7
default:
return 0
}
}