3

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
        });
4

1 回答 1

7

maxdate 的问题是您使用当前日期作为起点。然后你将 30 天添加到今天。

要修复使用parsed_date创建初始maxdate

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

否则,您还需要设置月份并将年份设置为当前日期,而不仅仅是设置日期

演示:http: //jsfiddle.net/2y67W/

于 2013-01-20T14:03:23.983 回答