I have previously asked this question and received helpful answers. Now, let's consider a business that operates from 9:00 AM to 5:00 PM PST time (California), closed on Saturdays and Sundays.
How can I make adjustments for this scenario?
The script below controls the display of an image based on the business hours. The image should show 'We're Open' at 9:00 AM PST and then change to 'We're Closed' at 5:00 PM. Any ideas on how to achieve this? Your input is appreciated!
Check out the reference Fiddle.
$(window).load(function(){
// Translate your hours to UTC, example here is using
// Central Standard Time (-0500 UTC)
// Opening hour in UTC is 16, Closing hour is 0 the next day
var d = new Date(),
open = new Date(),
closed = new Date();
// Statically set UTC date for open
open.setUTCHours(16);
open.setUTCMinutes(0);
open.setUTCSeconds(0);
open.setUTCMilliseconds(0);
// Statically Set UTC date for closing
closed.setUTCDate(d.getUTCDate()+1); // UTC time rotates back to 0, add a day
closed.setUTCHours(0); // UTC hours is 0
closed.setUTCMinutes(0);
closed.setUTCSeconds(0);
closed.setUTCMilliseconds(0);
// Debugging
console.log("user's date:" + d);
console.log("store open time in user's timezone:" + open);
console.log("store close time in user's timezone:" + closed);
console.log(d > open); // user's time is greater than opening time
console.log(d < closed); // is user's time less than closing time
// (you don't have to go home...)
// Test for store open?
if (d > open && d < closed) {
setOpenStatus(true);
} else {
setOpenStatus(false);
}
function setOpenStatus(isOpen) {
$('#opend').toggle(isOpen);
$('#closed').toggle(!isOpen);
}
});
EDITED/UPDATED SCRIPT
$(window).load(function(){
// Translate your hours to UTC, example here is using
// Central Standard Time (-0500 UTC)
// Opening hour in UTC is 16, Closing hour is 0 the next day
var d = new Date(),
open = new Date(),
closed = new Date();
// Statically set UTC date for open
open.setUTCHours(16);
open.setUTCMinutes(0);
open.setUTCSeconds(0);
open.setUTCMilliseconds(0);
// Statically Set UTC date for closing
closed.setUTCDate(d.getUTCDate()+1); // UTC time rotates back to 0, add a day
closed.setUTCHours(0); // UTC hours is 0
closed.setUTCMinutes(0);
closed.setUTCSeconds(0);
closed.setUTCMilliseconds(0);
// Debugging
console.log("user's date:" + d);
console.log("store open time in user's timezone:" + open);
console.log("store close time in user's timezone:" + closed);
console.log(d > open); // user's time is greater than opening time
console.log(d < closed); // is user's time less than closing time
// (you don't have to go home...)
// Test for store open?
if (d > open && d < closed) {
setOpenStatus(true);
}
if (d.getDay() !== 0 && d.getDay() !== 6 && (d > open && d < closed))
else {
setOpenStatus(false);
}
function setOpenStatus(isOpen) {
$('#opend').toggle(isOpen);
$('#closed').toggle(!isOpen);
}
});