Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable dates before today in jQuery datepicker

How do I disable dates before today in jQuery datepicker WITHOUT using minDate: 0?

I would like to enable navigation of the calendar as per usual before today while making sure that user do NOT pick dates before today.

(i.e. say today's date is 11Aug11 and I would like all the dates before this disabled but still enabling user to go to previous months, years, etc.)

like image 271
user864600 Avatar asked Aug 10 '11 15:08

user864600


People also ask

How to disable previous date in datepicker jQuery?

$("#datetimepicker2"). datepicker({ minDate: 0 }); jquery.

How do I turn off past dates on calendar?

How do I turn off past date in calendar? the previous dates we need to set the minDate property of the date picker. if we set minDate:0 then it will disable all the previous dates. and we set input attribute min:current_date then it will disable all the previous dates.

How to disable dates in datepicker html?

How do I restrict future dates in Datepicker? To disable future dates in JQuery date picker, we need to set the maxDate property. maxDate : 0 then all the future dates will be disabled. maxDate : '+1w' then all the future dates after a week will be disabled.

How to disable dates in DateRangePicker?

DateRangePicker can be inactivated on a page, by setting enabled value as false that will disable the component completely from all the user interactions including in form post.


2 Answers

While I agree that it's weird behavior, you might be able to fake it using the onSelect event of the datepicker.

$(document).ready(function() {
    $('#Date').datepicker({
        onSelect: function(dateText, inst) {
            //Get today's date at midnight
            var today = new Date();
            today = Date.parse(today.getMonth()+1+'/'+today.getDate()+'/'+today.getFullYear());
            //Get the selected date (also at midnight)
            var selDate = Date.parse(dateText);

            if(selDate < today) {
                //If the selected date was before today, continue to show the datepicker
                $('#Date').val('');
                $(inst).datepicker('show');
            }
        }
    });
});

Basically, you handle the onSelect event.

When a date is selected, check to see if it's before today's date.

If it is, then you immediately show the datepicker again and clear out the input box attached to it.


Updated Code sample is now completely functional. Here's a jsfiddle to demonstrate.

like image 88
Mark Biek Avatar answered Sep 18 '22 11:09

Mark Biek


Hope this is still helpful to someone. My solution is to place checks on individual dates like this:

$("#StartDate").datepicker({ dateFormat: 'yy-mm-dd',  beforeShowDay: NotBeforeToday});



function NotBeforeToday(date)
{
    var now = new Date();//this gets the current date and time
    if (date.getFullYear() == now.getFullYear() && date.getMonth() == now.getMonth() && date.getDate() > now.getDate())
        return [true];
    if (date.getFullYear() >= now.getFullYear() && date.getMonth() > now.getMonth())
       return [true];
     if (date.getFullYear() > now.getFullYear())
       return [true];
    return [false];
}

I find it simple and elegant

like image 23
memory of a dream Avatar answered Sep 22 '22 11:09

memory of a dream