Reputation: 37
Trying to use HTML / JS to display something like this: It's Monday 11:03 AM — We are currently open
I have tried a few different things but, can only get this to work partially. I want to show weekday/time of day/ and if the shop is open or closed.
These would be the hours:
Monday 10 a.m. - 5 p.m.
Tuesday 12 - 7 p.m.
Wednesday 10 a.m. - 5 p.m.
Thursday 10 a.m. - 5 p.m.
Friday 10 a.m. - 5 p.m.
Saturday 10 a.m. - 2 p.m.
Sunday Closed
function getDayOfWeek(date) {
const dayOfWeek = new Date(date).getDay();
var dateWithouthSecond = new Date();
dateWithouthSecond.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
return isNaN(dayOfWeek) ? null :
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][dayOfWeek];
}
document.getElementById("demo").innerHTML = ( "It's " + getDayOfWeek(Date.now()) );
Upvotes: 1
Views: 879
Reputation: 48600
As others have suggested, you could use Date.prototype.toLocaleString()
and pass the weekday
parameter to retrieve the day of the week.
const OperatingHours = {
'Monday' : { 'open': 10, 'close': 17 },
'Tuesday' : { 'open': 12, 'close': 19 },
'Wednesday' : { 'open': 10, 'close': 17 },
'Thursday' : { 'open': 10, 'close': 17 },
'Friday' : { 'open': 10, 'close': 17 },
'Saturday' : { 'open': 10, 'close': 14 },
'Sunday' : { 'open': 1, 'close': -1 }
};
const greeting = (date) => {
const now = new Date(),
weekday = now.toLocaleString('en-us', { weekday: 'long' }),
formattedTime = now.toLocaleTimeString('en-US',
{ hour: '2-digit', minute: '2-digit' }),
currHour = now.getHours(),
{ open, close } = OperatingHours[weekday],
isOpen = currHour >= open && currHour <= close;
return isOpen
? `It's ${weekday} ${formattedTime} — we are currently open.`
: `Sorry, it's ${weekday} ${formattedTime} — we are currently closed.`;
};
console.log(greeting(new Date()));
Upvotes: 2
Reputation: 11613
Set up an object w/the hours you're open, and then compare like below. Note that Sunday is setup so there is no value that is in between.
const HOURS_OPEN = {
"Monday": {"open": 10,"close": 16},
"Tuesday": {"open": 12,"close": 18},
"Wednesday": {"open": 10,"close": 16},
"Thursday": {"open": 10,"close": 16},
"Friday": {"open": 10,"close": 16},
"Saturday": {"open": 10,"close": 13},
"Sunday": {"open": 1,"close": -1}
}
let today = new Date();
let weekday = today.toLocaleString('en-us', {
weekday: 'long'
});
let currentHour = today.getHours();
let open = HOURS_OPEN[weekday].open;
let close = HOURS_OPEN[weekday].close;
let statusString = "";
if (currentHour >= open && currentHour <= close) {
statusString = `It's ${formatNow()}. We're open!`;
console.log(statusString);
} else {
statusString = `It's ${formatNow()}. Sorry, we're closed :(`;
console.log(statusString);
}
document.getElementById("demo").innerText = statusString;
function formatNow() {
let d = new Date();
let min = d.getMinutes();
let hr = d.getHours();
let ampm = "AM"
if (min < 10) {
min = "0" + min;
}
if (hr > 12) {
hr -= 12;
ampm = "PM";
}
return (`${hr}:${min} ${ampm}`);
}
<div id="demo"></div>
Upvotes: 1