Link to home
Start Free TrialLog in
Avatar of peps03
peps03

asked on

Exclude holidays when counting days in jQuery datepicker range

Hi,

This piece of code reduces the weekends from a range of selected days in jQuery datepicker:

var start = new Date($("#startdate").val().split("/").reverse().join(","));
var end = new Date($("#enddate").val().split("/").reverse().join(","));

var weekend_count = 0;
for (i = start.valueOf(); i <= end.valueOf(); i+= 86400000){
	var temp = new Date(i);
	if (temp.getDay() == 0 || temp.getDay() == 6) {
		weekend_count++;
	}
}				
$("#days").val((end - start) / 86400000 + 1 - weekend_count);        

Open in new window


I would like to alter it so also specific holidays that fall within the selected range are reduced:
var currentYear = (new Date).getFullYear();
var disabledDays = ["1-1-"+currentYear,"25-12-"+currentYear,"26-12-"+currentYear];

Open in new window


How should i alter the code?
Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Mrunal
Mrunal
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peps03
peps03

ASKER

Thanks for your reaction mroonal!

I see it working. but not correct, i use a European calendar, maybe that's the problem?

i use: dd-mm-yyyy. Could that explain the different outcome? what should be changed?
Avatar of peps03

ASKER

when using:

var startDate = "16/7/2012", endDate = "23/7/2012";// (16 july to 23 july)
var currentYear = (new Date).getFullYear();
var disabledDays = ["3/1/" + currentYear, "4/1/" + currentYear, "18/7/" + currentYear];


i get NaN as answer

of sometime negative numbers, like -50
Of course you are getting NaN as answer.
The reason behind this is you are entering date in mm/dd/yyyy and compiler treating it as mm/dd/yyyy.

So for validating 16 and 23 as month numbers, it fails and results as NaN.

Better way to handle this issue is, convert dd/mm/yyy into mm/dd/yyyy through some common function.
Hope this helps.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peps03

ASKER

Many Thanks! It works now!!