var hoursOfOperation = [
[ [8, 15], [17, 30] ], // Sunday
[ [8, 15], [17, 30] ], // Monday
[ [8, 15], [17, 30] ], // Tuesday
[ [8, 15], [17, 30] ], // Wednesday
[ [8, 15], [17, 30] ], // Thursday
[ [8, 15], [17, 30] ], // Friday
[ [8, 15], [17, 30] ] // Saturday
];
var date = new Date();
var timezone_EST = -300;
var open = isOpen(hoursOfOperation, date, timezone_EST);
document.body.innerHTML = open ? 'OPEN' : 'CLOSED';
function isOpen(hoursOfOperation, date, timezoneOffset) {
var hasOffset = timezoneOffset !== undefined;
var hour = hasOffset ? date.getUTCHours() : date.getHours();
var minute = hasOffset ? date.getUTCMinutes() : date.getMinutes();
var day = hasOffset ? date.getUTCDay() : date.getDay();;
var offsetHours = hasOffset ? Math.floor(timezoneOffset / 60) : 0;
var offsetMinutes = hasOffset ? timezoneOffset % 60 : 0;
var incrementHour = 0;
var incrementDay = 0;
if (offsetMinutes != 0) {
incrementHour = minute + offsetMinutes > 60 ? 1 : minute + offsetMinutes < 0 ? -1 : 1;
minute = mod(minute + offsetMinutes, 60);
}
if (offsetHours != 0) {
incrementDay = hour + offsetHours > 24 ? 1 : hour + offsetHours < 0 ? -1 : 1;
hour = mod(hour + offsetHours, 24);
}
day = mod(day + incrementDay, 7);
console.log(day, hour, minute);
return (function(dayOfWeek) {
var minTime = dayOfWeek[0];
var minHour = minTime[0];
var minMinute = minTime[1];
if (hour < minHour) return false;
if (hour === minHour && minute < minMinute) return false;
var maxTime = dayOfWeek[1];
var maxHour = maxTime[0];
var maxMinute = maxTime[1];
if (hour > maxHour) return false;
if (hour === maxHour && minute > maxMinute) return false;
return true;
}(hoursOfOperation[day]))
}
function mod(n, m) {
return ((n % m) + m) % m;
}