Using this javascript code allows me to check if a business is open based on its operating hours. It's effective for times like 17:00-23:00, but how can I modify it to work for hours past midnight, such as 0:30 or 1:00 in the morning?
var d = new Date();
var n = d.getDay();
var now = d.getHours() + "." + d.getMinutes();
var weekdays = [
["Sunday", 9.30, 12.00, 15.30,22.00],
["Monday", 8.30, 12.00, 15.30,19.00],
["Tuesday", 8.30, 12.00, 15.30,19.00],
["Wednesday", 8.30, 12.00, 15.30,19.00],
["Thursday", 8.30, 12.00, 15.30,19.00],
["Friday", 8.30, 11.30],
["Saturday"] // we are closed, sorry!
];
var day = weekdays[n];
if (now > day[1] && now < day[2] || now > day[3] && now < day[4]) {
console.log("We're open right now!");
}
else {
console.log("Sorry, we're closed!");
}
I understand that this script doesn't account for timezones, but since it's designed for local businesses, which operate in the same timezone as their clients, this isn't an issue.
Thanks in advance. PS: The original script was adapted from another post on stackoverflow Javascript Store Opening Hours
UPDATED VERSION: I made changes to the script because I now retrieve opening hours data from Firebase in JSON format, eliminating the need for the days array.
<script>
var d = new Date();
var n = d.getDay();
var now = d.getHours() + "." + d.getMinutes();
// Data retrieved from firebase
var day = {
"from": 8.30,
"to": 10,
"from2": 14.30,
"to2": 23
};
console.log('Now: ' + now);
if (now > day.from && now < day.to || now > day.from2 && now < day.to2) {
console.log("We're open right now!");
} else {
console.log("Sorry, we're closed!");
}
</script>
However, this setup fails when dealing with time ranges like 17:00-01:00.