I am trying to have a date Range select using the UI date picker.
in the from/to field people should not be able to view or select dates previous to the present day.
This is my code:
$(function() {
var dates = $( "#from, #to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
onSelect: function( selectedDate ) {
var option = this.id == "from" ? "minDate" : "maxDate",
instance = $( this ).data( "datepicker" ),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
dates.not( this ).datepicker( "option", option, date );
}
});
});
Can some one tell me how to disable dates previous the to the present date.
set startDate attribute of datepicker, it works, below is the working code
new Date()
: function get the todays date previous date are locked. 100% workingminDate:0
works for me.you have to declare current date into variables like this
Just to add to this:
If you also need to prevent the user to manually type a date in the past, this is a possible solution. This is what we ended up doing (based on @Nicola Peluchetti's answer)
What this does is to change the value to today's date if the user manually types a date in the past.
jQuery API documentation - datepicker
The minimum selectable date. When set to
null
, there is no minimum.Multiple types supported:
Date: A date object containing the minimum date.
Number: A number of days from today. For example
2
representstwo days
from today and-1
representsyesterday
.String: A string in the format defined by the
dateFormat
option, or a relative date.Relative dates must contain value and period pairs; valid periods are
y
foryears
,m
formonths
,w
forweeks
, andd
fordays
. For example,+1m +7d
representsone month and seven days
fromtoday
.In order not to display previous dates other than today