Skip to content

Instantly share code, notes, and snippets.

@hendrikkao1
Created June 6, 2019 14:58
Show Gist options
  • Save hendrikkao1/efcd13050c8159e87864a9838c3a36be to your computer and use it in GitHub Desktop.
Save hendrikkao1/efcd13050c8159e87864a9838c3a36be to your computer and use it in GitHub Desktop.
Function to check if current day and time fit the opening hours
/**
* Function to check if current day and time fit the opening hours.
* @param {number[]} openDays - Array of days represented as numbers, sunday - saturday : 0 - 6.
* @param {number} openingTime - Opening time in minutes, 9:00 : 9 * 60.
* @param {number} closingTime - Closing time in minutes 17:00 : 17 * 60.
* @return {boolean}
*/
function isOpeningHours(openDays = [1, 2, 3, 4, 5], openingTime = 9 * 60, closingTime = 17 * 60) {
const date = new Date();
const day = date.getDay();
const time = date.getHours() * 60 + date.getMinutes();
return openDays.includes(day) && openingTime <= time && time <= closingTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment