jQuery DatePicker Min Max dates

Rob picture Rob · Feb 1, 2013 · Viewed 76.1k times · Source

I have the jQuery date picker setup and working but would like help with setting the minDate and maxDate options. My current code is below (without these options). How can I set the minDate as 3 months before the defaultDate, and maxDate as 28days after the defaultDate?

var expdisp = $("#expdisp").attr("value");

$("#expirydate" ).datepicker({
    showOn: "button",
    buttonImage: "images/calendar.gif",
    buttonImageOnly: true,
    dateFormat: "dd/mm/yy",
    defaultDate: expdisp,
    showOtherMonths: true,
    selectOtherMonths: true,
    changeMonth: true,
    changeYear: true,
});

Answer

MuhammadHani picture MuhammadHani · Feb 1, 2013
$(function() {

    $( "#datepicker" ).datepicker({ 
        changeYear: true,
        minDate: '-3M',
        maxDate: '+28D',
    });
});

JSFiddle Demo

UPDATE

You can calculate tour max and min valid dates from the default date, then assign it to the date picker.

var expdisp = $("#expdisp").attr("value");

$("#expirydate" ).datepicker({
    showOn: "button",
    buttonImage: "images/calendar.gif",
    buttonImageOnly: true,
    dateFormat: "dd/mm/yy",
    defaultDate: expdisp,
    showOtherMonths: true,
    selectOtherMonths: true,
    changeMonth: true,
    changeYear: true,

    minDate: '-3M',
    maxDate: '+28D',
});

Update Demo