The University Library where I work is in the process of redesigned our home page. One of the features we wish to include is a widget to display the current hours. Unfortunately for a variety of reasons this must be done using client side scripting instead of a PHP script which pulls from Google Calendar. After much wailing and gnashing of teeth I have created such a script. It uses functions to see check if key dates fall within certain ranges and then assigns hours based on those results. After error checked it by manually setting the date assigned to the variable current everything seems fine. But its given me enough trouble that I want more experienced eyes to look at it and see if there any lingering issues I missed.
The script works as follows. After getting the current date and day of the week, the library's hours are assigned to a set of arrays. The set of variables which follows are the dates at which our schedule changes. Making each one a date object (as opposed to a number) seems to be the only way to compare apples to apples and not get an erroneous result. Also it makes it easier for non-programmers to update the script. In the functions which follows the current date is compared against these an if it falls within that range, the function returns true. The exception is the short_hours
function which must exist because there are a handful of days out of the year when we are open from 7:30 AM to 5PM. Lastly the functions are called using a set of if
statements and the one which is true sets the current hours from one of the arrays.
Flame away!
http://jsfiddle.net/mjcodelib/XZF59/
//Code written by Michael Paulmeno
var current = new Date(); //creates new date object
var weekDay = current.getDay(); //gets day of week (0-6 in Javascript)
var hours = document.getElementById("hours"); //stores code to simplify if statements
//The variables below set the library's operating hours
var hours_regular = ["Sunday's Hours: 2:00PM to 10:00PM", "Monday's Hours: 7:30AM to 10:00PM", "Tuesday's Hours: 7:30AM to 10:00PM", "Wednesday's Hours: 7:30AM to 10:00PM","Thursday's Hours: 7:30AM to 10:00PM", "Friday's Hours: 7:30AM to 4:00PM", "Closed Today"];
var hours_exam = ["Sunday's Hours: 2:00PM to 12:00PM", "Monday's Hours: 7:30AM to 12:00PM", "Tuesday's Hours: 7:30AM to 12:00PM", "Wednesday's Hours: 7:30AM to 12:00PM", "Thursday's Hours: 7:30AM to 12:00PM", "Friday's Hours: 7:30AM to 4:00PM", "Closed Today"];
var hours_intercession = ["Closed Today", "Monday's Hours: 8:00AM to 4:30PM", "Tuesday's Hours: 8:00AM to 4:30PM", "Wednesday's Hours: 8:00AM to 4:30PM", "Thursday's Hours: 8:00AM to 4:30PM", "Friday's Hours: 8:00AM to 4:00PM", "Closed Today", "Today's Hours: 8am - 5pm"];
var hours_august_intercession = ["Closed Today", "Monday's Hours: 8:00AM to 5:00PM", "Tuesday's Hours: 8:00AM to 5:00PM", "Wednesday's Hours: 8:00AM to 5:00PM", "Thursday's Hours: 8:00AM to 5:00PM", "Friday's Hours: 8:00AM to 4:00PM", "Closed Today"];
var hours_shortened = "Today's Hours: 7:30AM to 5:00PM";
//These are the 'important dates' for the year. At each of these dates our operating hours change. The format is year + month + date.
//Change the values in parenthesis every year to reflect the important dates for the next academic year.
//Note that in Javascript counting begins at 0, so January is month 0 and December is month 11.
//Fall Dates
var fall_semester_begin = new Date (2014,7,18);
var fall_break_begin = new Date (2014,9,15);
var fall_break_end = new Date (2014,9,20);
var fall_exams = new Date (2014,11,8);
var thanksgiving_break = new Date (2014,10,24);
var thanksgiving_break_end = new Date (2014,11,1);
var winter_intercession = new Date (2014,11,14);
var winter_holidays = new Date (2015,11,23);
//Spring Dates
var winter_intercession2 = new Date (2015,0,1);
var spring_semester_begin = new Date (2015,0,12);
var spring_break_begin = new Date (2015,2,9);
var spring_break_end = new Date (2015,2,16);
var spring_exams = new Date (2015,4,4);
//Summer Dates
var may_intercession = new Date (2014,4,11);
var summer_hours_regular = new Date(2014,5,2);
var august_intercession = new Date (2014,6,31);
//Do not touch anything below this line.
//The functions below compare the current date as formatted above to the important dates to determine at what point in the year the current date falls in.
function regular_hours_spring (today) {
if (today >= spring_break_end && today < spring_exams ||
today >= spring_semester_begin && today < spring_break_begin )
{
return true; }
else {
return false;
}
}
function regular_hours_summer (today){
if (today >= summer_hours_regular &&
today < august_intercession)
{
return true;}
else {return false;}
}
function regular_hours_fall (today) {
if (today >= thanksgiving_break_end && today < fall_exams ||
today >= fall_break_end && today < thanksgiving_break ||
today >= fall_semester_begin && today < fall_break_begin)
{
return true; }
else {
return false;
}
}
function exam_hours (today) {
if (today >= fall_exams && today <= winter_intercession ||
today >= spring_exams && today <= may_intercession)
{
return true; }
else {
return false;
}
}
function intercession_hours (today) {
if (today >= winter_intercession && today < winter_holidays ||
today >= thanksgiving_break && today < thanksgiving_break_end ||
today > fall_break_begin && today < fall_break_end ||
today >= august_intercession && today < fall_semester_begin ||
today >= may_intercession && today < summer_hours_regular ||
today > spring_break_begin && today < spring_break_end ||
today > winter_intercession2 && today < spring_semester_begin)
{
return true; }
else {
return false;
}
}
function short_hours (today) {
if (fall_break_begin && today > fall_semester_begin)
{
return true; }
else {
return false; }
}
function closed (today) {
if (today >= winter_holidays)
{
return true;
}
else {
return false; }
}
//The value returned by the functions above is logged to the error console for troubleshooting
console.log (regular_hours_spring(current));
console.log (regular_hours_summer(current));
console.log (regular_hours_fall(current));
console.log (exam_hours(current));
console.log (intercession_hours(current));
console.log (short_hours(current));
console.log (closed(current));
//The if statements below call the functions above and determine whether the hours displayed should be regular, exam, or intercession.
if (closed (current) && current >= winter_holidays) {
hours.innerHTML = "Closed Today";
}
else if(intercession_hours(current) && current >= august_intercession && current < fall_semester_begin) {
hours.innerHTML = hours_august_intercession[weekDay];
}
else if(intercession_hours(current)) {
hours.innerHTML = hours_intercession[weekDay];
}
else if (exam_hours (current)) {
hours.innerHTML = hours_exam[weekDay];
}
else if(regular_hours_spring(current)){
hours.innerHTML = hours_intercession[weekDay];
}
else if (regular_hours_summer (current)){
hours.innerHTML = hours_regular[weekDay];
}
else if (regular_hours_fall (current)){
hours.innerHTML = hours_regular[weekDay];
}
else if (short_hours ()) {
hours.innerHTML = hours_shortened;
}
else {hours.innerHTML = "<a href='http://www.deltastate.edu/academics/libraries/libraries-hours-of-operation/' target='_blank'>Click here for Library hours</a>"};