Skip to content

Instantly share code, notes, and snippets.

@csandman
Created May 29, 2019 18:55
Show Gist options
  • Save csandman/b7aaad5edffb2a522f3b5a8c6decf9dc to your computer and use it in GitHub Desktop.
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
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