Last active
August 26, 2024 03:46
-
-
Save dosbol/280f19c538c92e40966aad3dfbbd31e5 to your computer and use it in GitHub Desktop.
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
const openingHoursDiff = (workHours, date) => { | |
let [currentHour, currentMinute] = [date.getHours(), date.getMinutes()] | |
let current = currentHour * 100 + currentMinute | |
let {open, close} = workHours | |
let [openHour, openMinute] = [Math.floor(open / 100), open % 100] | |
let [closeHour, closeMinute] = [Math.floor(close / 100), close % 100] | |
let openMidnight = openHour == 0 && currentHour == 23 | |
openHour = openMidnight ? 24 : openHour | |
let opening = openMidnight ? 24 * 100 + openMinute : open | |
let closeMidnight = closeHour == 0 && currentHour == 23 | |
closeHour = closeMidnight ? 24 : closeHour | |
let closing = closeMidnight ? 24 * 100 + closeMinute : close | |
switch(true) { | |
case (opening - 100 == current): | |
return `Opens in 1 hour` | |
case (opening - 100 < current) && (current < opening): | |
return `Opens in ${ openMinute - currentMinute + (openHour==currentHour?0:60) } minutes` | |
case (opening <= current) && (current < closing - 100): | |
return `Open` | |
case (closing - 100 < current) && (current < closing): | |
return `Closes in ${ closeMinute- currentMinute + (closeHour==currentHour?0:60) } minutes` | |
case (closing - 100 == current): | |
return `Closes in 1 hour` | |
case true: | |
return `Closed` | |
} | |
} | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 7:59')) | |
// 'Closed' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 8:00')) | |
// 'Opens in 1 hour' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 8:01')) | |
// 'Opens in 59 minutes' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 8:30')) | |
// 'Opens in 30 minutes' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 8:59')) | |
// 'Opens in 1 minutes' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 9:00')) | |
// 'Open' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 17:29')) | |
// 'Open' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 17:30')) | |
// 'Closes in 1 hour' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 17:31')) | |
// 'Closes in 59 minutes' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 18:00')) | |
// 'Closes in 30 minutes' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 18:29')) | |
// 'Closes in 1 minutes' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 18:30')) | |
// 'Closed' | |
openingHoursDiff({open: 900, close:1830}, new Date('08.23.2024 00:00')) | |
// 'Closed' | |
openingHoursDiff({open: 1830, close:30}, new Date('08.23.2024 23:29')) | |
// 'Open' | |
openingHoursDiff({open: 1830, close:30}, new Date('08.23.2024 23:30')) | |
// 'Closes in 1 hour' | |
openingHoursDiff({open: 1830, close:30}, new Date('08.23.2024 23:31')) | |
// 'Closes in 59 minutes' | |
openingHoursDiff({open: 1830, close:30}, new Date('08.23.2024 00:00')) | |
// 'Closes in 30 minutes' | |
openingHoursDiff({open: 1830, close:30}, new Date('08.23.2024 00:29')) | |
// 'Closes in 1 minutes' | |
openingHoursDiff({open: 1830, close:30}, new Date('08.23.2024 00:30')) | |
// 'Closed' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment