如何限制jQuery UI Datepicker在将来禁止日期?

per*_*ai1 8 jquery jquery-ui datepicker

我正在使用jQuery UI Datepicker,因此只能选择星期日.

我想要的是,可以选择当前日期到未来的日期.这是我目前使用的代码:

var daysToDisable = [1,2,3,4,5,6];

        $('#startdate').datepicker({
            beforeShowDay: disableSpecificWeekDays
        });

        function disableSpecificWeekDays(date) {
            var day = date.getDay();
            for (i = 0; i < daysToDisable.length; i++) {
                if ($.inArray(day, daysToDisable) != -1) {
                    return [false];
                }
            }
            return [true];
        }
Run Code Online (Sandbox Code Playgroud)

小智 32

我只想使用单个条目将日期限制为今天以外的任何内容:

$('#startdate').datepicker({
    maxDate: '+0d'
});
Run Code Online (Sandbox Code Playgroud)

  • 由于它的简单性,这似乎是最好的答案. (3认同)

Cal*_*lum 4

您可以使用maxDate属性来阻止选择未来日期。

注意:这假设您的日期格式dd/mm/yy

编辑: http: //jsfiddle.net/jXGZz/

var fullDate = new Date();

var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);

$('#startdate').datepicker({
    maxDate: fullDate.getDate() + "/" + twoDigitMonth + "/" + fullDate.getFullYear(),
    beforeShowDay: disableSpecificWeekDays
});


function disableSpecificWeekDays(date) {
    if(date.getDay()==0){
        return [true];
    }else{
        return [false];
    }
}
Run Code Online (Sandbox Code Playgroud)

修改后的两位数字月份

var twoDigitMonth = fullDate.getMonth()+1<10 ? "0"+fullDate.getMonth()+1 : fullDate.getMonth()+1 ;
Run Code Online (Sandbox Code Playgroud)