Skip to content Skip to sidebar Skip to footer

Create A Specific Date Range With Jquery Datepicker

I'm trying to use the jQuery date picker to create a start date calendar and an end date calender. I'm using the 'date range' example seen here: http://jqueryui.com/demos/datepicke

Solution 1:

There you go : http://jsfiddle.net/c0mm0n/SJhmF/3/

$(function() {
    $( "#from, #to" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onSelect: function( selectedDate ) {
            if(this.id == 'from'){
              var dateMin = $('#from').datepicker("getDate");
              var rMin = newDate(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 1); // Min Date = Selected + 1dvar rMax = newDate(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 31); // Max Date = Selected + 31d
              $('#to').datepicker("option","minDate",rMin);
              $('#to').datepicker("option","maxDate",rMax);                    
            }

        }
    });
});

Solution 2:

This should do it:

http://jsfiddle.net/abze4/

$(function() {
    var fromDate = $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 2,
        minDate: newDate(),
        onSelect: function(selectedDate) {
            var instance = $(this).data("datepicker");
            var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
            date.setDate(date.getDate()+30);
            toDate.datepicker("option", "minDate", date);
        }
    });

    var toDate = $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 2
    });
});

Basically this sets the from date's minDate to today. Then when you select a fromDate, it sets the minDate of the toDate to the selected date +30.

Post a Comment for "Create A Specific Date Range With Jquery Datepicker"