Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict jQuery UI Datepicker to disallow dates in the future?

I am using a jQuery UI Datepicker, such that only Sunday can be selected.

What I would like to happen is have it where no dates from the current date into the future can be selected. Here is the code I am using currently:

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];
        }
like image 524
pertrai1 Avatar asked Aug 03 '11 12:08

pertrai1


2 Answers

I would just use the single entry to restrict the date to nothing beyond today:

$('#startdate').datepicker({
    maxDate: '+0d'
});
like image 130
awilbourn Avatar answered Sep 20 '22 15:09

awilbourn


You can use the maxDate property to prevent the selection of future dates.

Note: this assumes your dateFormat is dd/mm/yy

Edit: 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];
    }
}

Modified twoDigitMonth

var twoDigitMonth = fullDate.getMonth()+1<10 ? "0"+fullDate.getMonth()+1 : fullDate.getMonth()+1 ;
like image 41
Calum Avatar answered Sep 21 '22 15:09

Calum