Wow this is so incredibly helpful!! Thank you so much @paul2009!!
I fiddled around with the code you created and made a version that accounts for holidays. I thought I'd share it here in case it helps anyone else.
Also, I'm not a very experienced coder, so I'd love to learn if there's a more efficient or better way to do this!
I added this part below "Store opening hours" to define the holidays:
// Define holidays (without the year) in "MM-DD" format
const holidays = [
"12-25", // Christmas Day
"01-01", // New Year's Day
// Add more holidays (MM-DD format) as needed
];
I added this after "Get the current day and time" to check if it's currently a holiday:
// Function to check if the date is a holiday (without the year)
function isHoliday(date) {
const month = date.getMonth() + 1; // Month is zero-based, so add 1
const day = date.getDate();
const monthDay = `${month < 10 ? '0' : ''}${month}-${day < 10 ? '0' : ''}${day}`;
return holidays.includes(monthDay);
}
Then I modified the last "Check if the store is open" section:
// Check if the store is open
const isHolidayToday = isHoliday(today);
if (isHolidayToday || storeHours[currentDay].open === "Closed" || storeHours[currentDay].close === "Closed") {
openingHours.textContent = "Sorry, the store is closed today.";
} else {
// Code to handle open hours
const openTime = storeHours[currentDay].open;
const closeTime = storeHours[currentDay].close;
openingHours.textContent = `Today's opening hours: ${openTime} - ${closeTime}`;
}
EDIT: Originally the code had to include the year. This edited version should work for the MM-DD format, so no year is required.