Skip to content

Instantly share code, notes, and snippets.

@nathanpc
Created August 25, 2022 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanpc/278e5f0e36f0f258f410acb5c8a84032 to your computer and use it in GitHub Desktop.
Save nathanpc/278e5f0e36f0f258f410acb5c8a84032 to your computer and use it in GitHub Desktop.
Simple store open/closed indicator
/**
* open-close.js
* Dynamic display of a store open state.
*
* @author Nathan Campos <nathan@innoveworkshop.com>
*/
// ==== Week Day Codes ====
// Domingo 0
// Segunda 1
// Terça 2
// Quarta 3
// Quinta 4
// Sexta 5
// Sábado 6
// ========================
jQuery(document).ready(function ($) {
var now = new Date();
var currentDay = now.getDay();
var openElement = $("#store-indicator-open");
var closedElement = $("#store-indicator-closed");
var timeZone = -3;
var officeHours = {
noWorkDays: [
0 // Domingo
],
regularHours: {
start: 7,
end: 21,
lunchBreak: {
start: 11,
end: 18
}
},
exceptionalDays: [
{
weekDay: 6, // Sabado
start: 8,
end: 12
}
]
};
// Ensure the elements start hidden.
openElement.hide();
closedElement.hide();
// Are we in a no work day?
if (officeHours.noWorkDays.some(function (day) {
if (currentDay === day) {
closedElement.show();
console.log("NO WORK DAY");
return true;
}
return false;
})) {
return;
}
// Are we in an exceptional day?
if (officeHours.exceptionalDays.some(function (day) {
if (currentDay === day.weekDay) {
var openingTime = new Date(Date.UTC(now.getFullYear(), now.getMonth(),
now.getDate(), day.start + timeZone, 00));
var closingTime = new Date(Date.UTC(now.getFullYear(), now.getMonth(),
now.getDate(), day.end + timeZone, 00));
var open = (now.getTime() > openingTime.getTime()) && (now.getTime() <
closingTime.getTime());
if (open) {
openElement.show();
console.log("EXCEPTION DAY OPEN");
} else {
closedElement.show();
console.log("EXCEPTION DAY CLOSE");
}
return true;
}
return false;
})) {
return;
}
// Looks like a regular day. Check if we are on a lunch break.
var lunchStartTime = new Date(Date.UTC(now.getFullYear(), now.getMonth(),
now.getDate(), officeHours.regularHours.lunchBreak.start + timeZone, 00));
var lunchEndTime = new Date(Date.UTC(now.getFullYear(), now.getMonth(),
now.getDate(), officeHours.regularHours.lunchBreak.end + timeZone, 00));
var closedForLunch = (now.getTime() > lunchStartTime.getTime()) && (now.getTime() <
lunchEndTime.getTime());
if (closedForLunch) {
console.log("REGULAR DAY LUNCH");
closedElement.show();
return;
}
// Only thing left to do is check if we are actually within office hours.
var openingTime = new Date(Date.UTC(now.getFullYear(), now.getMonth(),
now.getDate(), officeHours.regularHours.start + timeZone, 00));
var closingTime = new Date(Date.UTC(now.getFullYear(), now.getMonth(),
now.getDate(), officeHours.regularHours.end + timeZone, 00));
var open = (now.getTime() > openingTime.getTime()) && (now.getTime() <
closingTime.getTime());
if (open) {
console.log("REGULAR DAY OPEN");
openElement.show();
} else {
console.log("REGULAR DAY CLOSED");
closedElement.show();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment