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.)
$("#datetimepicker2"). datepicker({ minDate: 0 }); jquery.
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 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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With