Created
May 29, 2019 18:55
-
-
Save csandman/b7aaad5edffb2a522f3b5a8c6decf9dc to your computer and use it in GitHub Desktop.
Calculate if a place is open based on the hours returned from the Google Places API
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
function isOpenNow(place) { | |
const date = new Date(); | |
const day = date.getDay(); | |
const hours = date.getHours(); | |
const minutes = date.getMinutes(); | |
const currentTime = (day * 24 + hours) * 60 + minutes; | |
// calculate the current number of minutes into the week | |
let currentlyOpen = false; | |
const days = place.opening_hours.periods; | |
for (let i = 0; i < days.length; i++) { | |
const openTime = | |
(days[i].open.day * 24 + days[i].open.hours) * 60 + | |
days[i].open.minutes; | |
const closeTime = | |
(days[i].close.day * 24 + days[i].close.hours) * 60 + | |
days[i].close.minutes; | |
// check to see if the current time into the week is between the | |
// opening and closing time values for each day of the week | |
if (currentTime >= openTime && currentTime < closeTime) { | |
currentlyOpen = true; | |
break; | |
} | |
} | |
return currentlyOpen; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment