jquery datepicker
NickName:Ghatzi Ask DateTime:2011-08-30T23:32:54

jquery datepicker

I've looked at a few question on here and online but i cant seem to find the answer im looking for or maybe i just cant implement it right. I have 2 datepickers...a check in date and a check out date. The check out date must always be within 30 days of the check in date. I've managed to use the code below to get this result however its on an onSelect therefore it dosent work when the page is loaded, only when u select a check in date. By default the check in date is today therefore the check out should only be selectable for up to 30 days from when the page loads. Any help would be appreciated.

    var today = new Date();
today.setHours(0,0,0,0);
$(function(){$( "#datepicker, #departure" ).datepicker({
    defaultDate: +1,
    minDate: 0,
    maxDate: '+12m',
    dateFormat: 'dd/mm/yy',
    numberOfMonths: 3,
    showOn: 'both',
    buttonImageOnly: true,
    buttonImage: 'http://images.aboutrooms.com/hotels/graphics/calendar.gif',
    //beforeShowDay: function(date){return [date.getTime() != today.getTime(), ''];}
    onSelect: function( selectedDate ) {
        if(this.id == 'datepicker'){
          var dateMin = $('#datepicker').datepicker("getDate");
          var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 1); // Min Date = Selected + 1d
          var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 31); // Max Date = Selected + 31d
          $('#departure').datepicker("option","minDate",rMin);
          $('#departure').datepicker("option","maxDate",rMax);                    
        }

    }
        });
});

Copyright Notice:Content Author:「Ghatzi」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7245780/jquery-datepicker

Answers
Marek Sebera 2011-08-30T15:42:36

do it like this:\n\n\nmove that onSelect function to external function\nmake call on it when $(document).ready();\npoint to it from onSelect in datepicker configuration object \n\n\nfn:\n\nfunction datepickerhandle( selectedDate ) {\n if(this.id == 'datepicker'){\n var dateMin = $('#datepicker').datepicker(\"getDate\");\n var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 1); // Min Date = Selected + 1d\n var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 31); // Max Date = Selected + 31d\n $('#departure').datepicker(\"option\",\"minDate\",rMin);\n $('#departure').datepicker(\"option\",\"maxDate\",rMax); \n }\n\n }\n\n\nonSelect:\n\nonSelect: datepickerhandle,\n\n\nonLoad:\n\n$(document).ready(function(){ datepickerhandle([selected time]); });\n\n\nto get selected time see this SO question: jQuery DatePicker: Get Selected Date\n\n\n\nvariant no. 2 \n\njQuery datepicker set selected date, on the fly\n\nwhen document loads, set the date for datepicker instance on-fly to fire onSelected event",


More about “jquery datepicker” related questions

datepicker jquery ui and moment datepicker

I'm using moment datepicker moment datepicker to display a calendar, I have also in my code, the jquery-ui js file, the calendar works fine until I add the jquery-ui file, the problem is that both

Show Detail

jQuery ui datepicker conflict with bootstrap datepicker

I want to show two months using bootstrap datepicker. After search on google I am not able to find how to show multiple months using bootstrap datepicker. I found that I can use JQuery UI for displ...

Show Detail

jQuery ui datepicker conflict with bootstrap datepicker

I want to show two months using bootstrap datepicker. After search on google I am not able to find how to show multiple months using bootstrap datepicker. I found that I can use JQuery UI for displ...

Show Detail

JQuery DatePicker and beforeShowDay

I am desperately trying to use this feature to enable only specific days in my datepicker, but the beforeShowDay function is never triggered :( even this is not working: $(document).ready(functio...

Show Detail

JQuery Datepicker Dates

what I need is, when pressed the blank, display only June calendarie and either highlight from 2014-06-01 to 2014-06-07 or disable the rest of dates. I have seen posts about hightlighting and tried

Show Detail

jQuery - datepicker

I know there were tones of questions like this - but I can't get this working. <script type="text/javascript"> $(document).ready(function(){ $("#input-datepicker").datepicker(); ...

Show Detail

Jquery Datepicker is not working

I have added a jquery datepicker on asp.net page. When I click on any button, the page postbacks and then the jquery datepicker stop working (It does not appear when I click on textbox linked to Jq...

Show Detail

jquery datepicker formatting

I am trying to format a jquery datepicker to display as Saturday, 18 May, 2013 but also have a second hidden field that would submit 2013-05-18 so I can use it in a MySQL query. I cannot seem to ge...

Show Detail

Datepicker jquery ui-datepicker-trigger

I need to add two calendar jQuery objects to the html page. I have the below setting for date picker. Have an icon image for opening the calendar object. For absolute positioning the icon image do...

Show Detail

Jquery UI Datepicker not displaying

UPDATE I have reverted back to Jquery 1.3.2 and everything is working, not sure what the problem is/was as I have not changed anything else apart of the jquery and ui library versions. UPDATE END I

Show Detail