我需要使用jquery UI Datepicker计算两个日期之间的剩余天数,不包括Holidays和WeekDays(周六,周日)。
我已经成功计算了不包括工作日的剩余天数,但只是暂停了国家假期。如何在计算剩余天数时排除这些国定假日。
这是代码:
var natDays=[];
natDays[0]=[1, 27];
natDays[1]=[2, 6];
natDays[2]=[3, 17];
natDays[3]=[2, 7];
function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1
&& date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}
function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
function excludeWeekDaysOrHolidays(startDate, endDate) {
if (endDate < startDate){
return 0;
}
// Calculate days between dates
var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
startDate.setHours(0,0,0,1); // Start just after midnight
endDate.setHours(23,59,59,999); // End just before midnight
var diff = endDate - startDate; // Milliseconds between datetime objects
var days = Math.ceil(diff / millisecondsPerDay);
// Subtract two weekend days for every week in between
var weeks = Math.floor(days / 7);
var days = days - (weeks * 2);
// Handle special cases
var startDay = startDate.getDay();
var endDay = endDate.getDay();
// Remove weekend not previously removed.
if (startDay - endDay > 1)
days = days - 2;
// Remove start day if span starts on Sunday but ends before Saturday
if (startDay == 0 && endDay != 6)
days = days - 1
// Remove end day if span ends on Saturday but starts after Sunday
if (endDay == 6 && startDay != 0)
days = days - 1
//check if National Holiday is in between two dates
if (noWeekendsOrHolidays(date)[0]) {
days=nationalDays(date);
}
return days;
}
任何帮助将不胜感激!
另外,我正在尝试以下代码:
diff=endDate-startDate;
var holi=new Array("02-25-2013","05-22-2013","06-28-2013");
p=0;
hcnt=0;
while(p<holi.length)
{
holiday=new Date(holi[p]);
//alert(holiday);
if(diff>holiday-startDate)
hcnt++;
//alert(hcnt);
p++;
}
假日返回“无效日期”错误。 hcnt始终显示为0.
答案 0 :(得分:0)
试试这个:
new Date("YYYY-MM-DD");
而不是“MM-DD-YYYY”......
在你的情况下:
var holi = new Array("2013-02-25", "2013-05-22", "2013-06-28");