设置最大日期jQuery Datepicker [英] Setting max date jquery datepicker
问题描述
我很难设置jquery datepicker的最大日期.我想在最小日期前增加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.
我以dd.mm.yyyy格式获取日期.我拆分了日期,并创建了一个Date对象以将其用作主意(此方法有效).我的问题是我不能在maxdate属性上使用'+ 30D',并且我还尝试了将第二个日期对象作为maxdate而不起作用.
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.
我当前的无效代码:
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
});
推荐答案
maxdate的问题是您使用当前日期作为起点.然后您将今天增加30天.
The problem with maxdate is you are using current date as the start point. Then you add 30 days to today.
要修复此问题,请使用parsed_date
创建初始的maxdate
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
演示: http://jsfiddle.net/2y67W/
这篇关于设置最大日期jQuery Datepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!