Setting max date jquery datepicker

£可爱£侵袭症+ 提交于 2019-12-01 07:44:30

问题


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

回答1:


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/



来源:https://stackoverflow.com/questions/14425001/setting-max-date-jquery-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!