PDA

View Full Version : Customize the script to add holidays.



bhappyharsha
04-20-2011, 06:25 AM
1) Script Title: Dynamic Calendar

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex6/dcalendar.htm

3) Describe problem: I want to know if there is a way to add a list of holidays in a year to this script so that they too appear in red as a holiday.

Other than that, it's a simple, yet efficient script.

Thanks,
Harsha.

djr33
04-20-2011, 07:08 PM
if (date.getDay() == 0 || date.getDay() == 6)
document.layers["idMenuContainer"].document.layers["idDate"+nDate].color = "red";
else
//......

That line determines whether the day is a saturday or a sunday. If it is, then it becomes red. You should be able to check if that date is a holiday like this:

if (date.getDay() == 0 || date.getDay() == 6 || isHoliday(date))
That will also check if the date is a holiday, if it's not a saturday or a sunday.

Now this means that you need to add a new Javascript function called "isHoliday()". The exact way that this will work depends on how you want to store your holidays, but here's a basic (non-working) example:

function isHoliday(date) {
if (date_is_in_list_of_holidays) {
return true;
}
else {
return false;
}
}

Does that help? Now if you want to fully implement this you'll need to decide how you will store the dates. An array (or object?) is probably best. You can store them in a format like this: "MM-DD" then use this: date.getMonth()+'-'+date.getDate() to find the equivalent string from the current date.
(Note that it's actually (M)M-(D)D because it would be 1-1 for January first, for example.)

So basically something like this:

function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}
function isHoliday(date) {
var holidays = new Array('1-1','11-27','12-25');
if (inArray(date.getMonth()+'-'+date.getDate(),holidays)) {
return true;
}
else {
return false;
}
}