jQuery -To get minDate value and set that value
I have a date field and want to set minDate for the same.There is one custom range that i have defined for beforeShowDay that make some of the date as selectable.I want to set the first selectable date using setDate function. How can i achieve the same ?
e.g: when i use this on ready function, jQuery('#startDate').datepicker("setDate",new Date());so by the time page is loaded it is populated with StartDate:09/27/2010. Now instead of using new Date(), i want first selectable date to be populated.
Here is my code for the same:
<label>Start Date:</label><input type="text" id="startDate"></input>
<label>End Date:</label><input type="text" id="endDate"></input>
<script>jQuery(function() {
jQuery('#startDate, #endDate').datepicker();
jQuery('#startDate, #endDate').datepicker('option', {
beforeShow: customRange
});
//This populates with Values as current date in this format10/10/2010(I DONT WANT
//THIS...I WANT THE FIRST MINIMUM DATE of start date field TO BE SELECTABLE in this
//case it should be 10/11/2010 ??????????????????)
jQuery('#startDate').datepicker("setDate",new Date());
});
function customRange(input) {
if (input.id == 'endDate') {
//Get date from FromDate field
var startDate = jQuery('#startDate').datepicker("getDate");
//add 1 day in the From Date, so it become minDate for ToDate Field
var startDate_For_ToDate_Field = new Date(startDate.getFullYear(),
startDate.getMonth(), startDate.getDate()+1);
//add 30 dyas in the From Date,so it becomes maxDate for ToDate Field
var endDate_For_FromDate_Field= new D开发者_开发百科ate(startDate.getFullYear(), startDate.getMonth(),
startDate.getDate()+31);
return {
numberOfMonths: 2,
minDate: startDate_For_ToDate_Field,
maxDate: endDate_For_FromDate_Field,
beforeShowDay:$.datepicker.noWeekends
};
} else if (input.id == 'startDate') {
$('#endDate').datepicker("setDate",null);
return {
numberOfMonths: 2,
minDate:'+1',
maxDate: jQuery('#endDate').datepicker("getDate")+'7',
beforeShowDay:$.datepicker.noWeekends
};
}
}
</script><script type="text/javascript">
Thanks, Dhiren
$( ".selector" ).datepicker({ minDate: new Date(2007, 1 - 1, 1) });
for more info: http://jqueryui.com/demos/datepicker/#option-minDate
精彩评论