jQuery UI Datepicker - allow only certain weekdays

跟風遠走 提交于 2019-11-29 06:54:34
Jan Thomas
$( ".datepicker.future" ).datepicker('option','beforeShowDay',function(date){
    var td = date.getDay();
    var ret = [(date.getDay() != 0 && date.getDay() != 6),'',(td != 'Sat' && td != 'Sun')?'':'only on workday'];
    return ret;
});

@Jan Thomas forgot to add the variable td. The correct code is:

$( ".datepicker.future" ).datepicker('option','beforeShowDay',function(date){
    var td = date.getDay();
    var ret = [(date.getDay() != 0 && date.getDay() != 6),'',(td != 'Sat' && td != 'Sun')?'':'only on workday'];
    return ret;
});
$('selector').datepicker({
beforeShowDay: $.datepicker.noWeekends // disable weekends
});

From the docs:

beforeShowDay: The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable, [1] equal to a CSS class name(s) or '' for the default presentation, and [2] an optional popup tooltip for this date. It is called for each day in the datepicker before it is displayed.

For an example, please see here:

http://codeasp.net/blogs/raghav_khunger/microsoft-net/1088/jquery-datepicker-disable-specific-weekdays

Jean Mallet's answer (beforeShowDay: $.datepicker.noWeekends) is simple and elegant.

However, if you're already calling a function using beforeShowDay (as I was in my case), here's how you can disable certain days of the week (in this case, Sunday and Saturday), chained along with the rest of your function:

beforeShowDay: function (date) {

    /* your other function code here */

    if (date.getDay() > 0 && date.getDay() < 6) {
        return [true, ''];
    } else {
        return [false, ''];
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!