jQuery UI datepicker - 禁用公共假期,周末,上午10点后的第二天,仅允许周二,周三和周二星期四作为可选择的日子

时间:2015-01-22 12:10:35

标签: javascript jquery jquery-ui datepicker jquery-ui-datepicker

我正在使用JQuery UI Datepicker Disbale Next Day After 12pm中的代码来禁用周末,公众假期和第二天(如果在上午10点之后选择),但我仍然坚持如何仅允许选择星期二,星期三和星期四。

// dates
var dateMin = new Date();
var weekDays = AddWeekDays(1);
dateMin.setDate(dateMin.getDate() + weekDays);
var natDays = [
  [1, 1, 'uk'],
  [12, 25, 'uk'],
  [12, 26, 'uk']
];
function noWeekendsOrHolidays(date) {
    var noWeekend = $j.datepicker.noWeekends(date);
    if (noWeekend[0]) {
        return nationalDays(date);
    } else {
        return noWeekend;
    }
}
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 AddWeekDays(weekDaysToAdd) {
    var mydate = new Date();
    if (mydate.getHours()>=10) 
    var daysToAdd = 1;
    else var daysToAdd = 0;
    var day = mydate.getDay()
    weekDaysToAdd = weekDaysToAdd - (5 - day)
    if ((5 - day) < weekDaysToAdd || weekDaysToAdd == 1) {
        daysToAdd = (5 - day) + 2 + daysToAdd
    } else { // (5-day) >= weekDaysToAdd
        daysToAdd = (5 - day) + daysToAdd
    }
    while (weekDaysToAdd != 0) {
        var week = weekDaysToAdd - 5
        if (week > 0) {
            daysToAdd = 7 + daysToAdd
            weekDaysToAdd = weekDaysToAdd - 5
        } else { // week < 0
            daysToAdd = (5 + week) + daysToAdd
            weekDaysToAdd = weekDaysToAdd - (5 + week)
        }
    }

    return daysToAdd;
}   

$j('.input-text.addon.addon-custom').datepicker({
    beforeShowDay: noWeekendsOrHolidays,
    minDate : dateMin,
    defaultDate: +1,
    firstDay: 1,
    changeFirstDay: true,
    dateFormat: "DD, dd MM yy"
});

非常感谢任何帮助。

在这里小提琴:http://jsfiddle.net/prydonian/4k4gga6j/

2 个答案:

答案 0 :(得分:0)

在您的日期选择器功能上,添加“beforeShowDay”选项,如下所示。

jQuery('#datepicker').datepicker({
    minDate: dateMin,
    defaultDate: +1,
    firstDay: 1,
    changeFirstDay: true,
    dateFormat: "DD, dd MM yy",
    beforeShowDay: function(day){
         if (day.getDay()<2 || day.getDay()>4){
            return [false, ""];
        }
        return noWeekendsOrHolidays(day);
    }
});

这里是小提琴更新:http://jsfiddle.net/4k4gga6j/3/

答案 1 :(得分:0)

如果我理解你,你需要排除星期二,星期三和星期四以外的任何一天吗?如果答案是肯定的,那么您应该将以下代码添加到现有的noWeekendsOrHolidays方法中:

($.inArray(date.getDay(), [2, 3, 4]) != -1)

这里更新了jsFiddle http://jsfiddle.net/4k4gga6j/4/