Tuesday, July 13, 2010

Javascript In or Out of Office

Wanted to create a standard tool to determine if a business is in or out of the office. Fairly standard stuff, but here it is if anyone can use it.


Needed to check for 3 things.


  1. Holidays

  2. Weekends

  3. Business Hours (in this case 8 to 5)




Anyways, here's the script that results in a boolean value to determine whether or not the office is open.


var d = new Date();
var day = d.getDay();
var h = d.getHours();
var y = d.getYear();

var holidays = ['1/1/' + y,
'1/21/' + y,
'2/18/' + y,
'5/26/' + y,
'7/4/' + y,
'9/1/' + y,
'10/13/' + y,
'11/11/' + y,
'11/27/' + y,
'12/25/' + y,
'12/31/' + y];

function isWeekend() {
if (day == 0 || day == 6) {
return true;
}
else
{
return false;
}
}

function isOffHours() {
if (h < 8 || h > 17) {
return true;
}
else {
return false;
}
}

function businessClosed() {
var today = (d.getMonth() + 1) + '/' + d.getDate() + '/' + y;
if (isWeekend() == true || (jQuery.inArray(today, holidays)) > 0 || isOffHours() == true) {
return true;
}
else {
return false;
}
}

$(function () {
if (businessClosed() == true) {
alert('Sorry, we are closed!');
}
else {
alert('Open for Business!');
}
});


If you want to download, it can be found HERE.