ffffff01
ffffff01

Reputation: 5238

Setting max date jquery datepicker

I'm having a hard time setting the max date of the jquery datepicker. I want to add thirty days to the min date.

I get my date in dd.mm.yyyy format. I split the date and make a Date object to use this as mindate (this works). My problem is that I cant use '+30D' on the maxdate property, and I have also tried making a second date object as my maxdate without any effect.

My current code that does not work:

        var values = validdate.split(".");
        var parsed_date = new Date(values[2], values[1], values[0]);

        var maxdate = new Date();
        maxdate.setDate(parsed_date.getDate() + 30);

        $("#date").datepicker({
            changeMonth: true,
            changeYear: true,
            minDate: parsed_date,
            maxDate: maxdate
        });

Upvotes: 3

Views: 11533

Answers (1)

charlietfl
charlietfl

Reputation: 171669

The problem with maxdate is you are using current date as the start point. Then you add 30 days to today.

To fix use the parsed_date to create the initial maxdate

var maxdate = new Date(parsed_date);
maxdate.setDate(parsed_date.getDate() + 30);

Otherwise you would need to also set month and set year to current date, not just set date

DEMO: http://jsfiddle.net/2y67W/

Upvotes: 7

Related Questions