HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Date Comparison Script

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptdatecomparison

Problem

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 hour

Solution

I'm not an expert in JavaScript, but there is some things I can say about your code.
Naming

Unfortunately for a variety of reasons this must be done using client
side scripting instead of a PHP script which pulls from Google
Calendar

Even if it's not your first option and not the one you desire, you need to have the same standard as your regular. Have you read some style convention ? Almost all your variables names and functions names are not following any "standard" of JavaScript.
Jsfiddle

You provided a jsfiddle (which is nice), but do know that there is a TidyUp and JSHint buttons that will clear most of the commons (syntax and indentation) mistakes. I was surprised that there was still some errors left (only two but there should have been none).
Bad Idea

I don't know if you really have another option (as I don't if you have some database or what all your system looks like), but this right here is something you should evade at all cost!

//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);


As a maintainer, I don't want to open a script, change dates, repackage everything, tests everything to make sure I didn't broke something just for some dates. Worse, I will need to do it every year, since it's will never be the same thing. There is also a risk that nobody will think to change the values in time for the new session, which would be easily avoidable with an external source of date (since it would not need a code change).

You should be able to grab this data from an external source which will feed you the good values. A JSON object containing the data could be an option, querying a service which return you all you need could work too. If "this must be done using client side" means : I can't use an external source of data and I need to hard-code the values, then disregards this.

Code Snippets

//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);

Context

StackExchange Code Review Q#57599, answer score: 2

Revisions (0)

No revisions yet.