I'm working on an hours of operation display, which list the hours out:
- Monday - Friday / 8 - 4
- Saturday / 8 - 2
- Sunday / Closed
I have my script add a class to highlight the lines depending on which day of the week it is, but I wonder another way to complete the task. I would like it if I was able to define monday-friday as one instead of the entire list of them in classes.
var now = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var checkTime = function() {
var today = weekday[now.getDay()];
var timeDiv = document.getElementById('timeDiv');
var dayOfWeek = now.getDay();
var hour = now.getHours();
var minutes = now.getMinutes();
//add AM or PM
var suffix = hour >= 12 ? "PM" : "AM";
// add 0 to one digit minutes
if (minutes < 10) {
minutes = "0" + minutes
};
if ((dayOfWeek == 6) && hour >= 8 && hour <= 14) {
hour = ((hour + 11) % 12 + 1); //i.e. show 1:15 instead of 13:15
timeDiv.innerHTML = '' + today + ' ' + hour + ':' + minutes + suffix + ' - we\'re open!';
timeDiv.className = 'open';
}
else if ((dayOfWeek == 1 || dayOfWeek == 2 || dayOfWeek == 3 || dayOfWeek == 4 || dayOfWeek == 5) && hour >= 8 && hour <= 16) {
hour = ((hour + 11) % 12 + 1);
timeDiv.innerHTML = 'Hi!! ' + today + ' ' + hour + ':' + minutes + suffix + ' - we\'re open!';
timeDiv.className = 'open';
}
else {
if (hour == 0 || hour > 12) {
hour = ((hour + 11) % 12 + 1); //i.e. show 1:15 instead of 13:15
}
timeDiv.innerHTML = ' Sorry—we\'re closed!';
timeDiv.className = 'closed';
}
};
var currentDay = weekday[now.getDay()];
var currentDayCLASS = "." + currentDay; //gets todays weekday and turns it into class
$(currentDayCLASS).toggleClass("today"); //hightlights today in the view hours
setInterval(checkTime, 1000);
checkTime();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="hours-block">
<h2>Hours</h2>
<div id="Monday" class="dateTime Monday Tuesday Wednesday Thursday Friday">
<div class="day">Monday - Friday</div>
<div class="time">8:00am - 4:00pm</div>
</div>
<br>
<div id="Saturday" class="dateTime Saturday">
<div class="day">Saturday</div>
<div class="time">8:00am - 2:00pm</div>
</div>
<br>
<div id="Sunday" class="dateTime Sunday">
<div class="day">Sunday</div>
<div class="time">Closed</div>
</div>
</div>