2012-04-13 99 views
39

您好我想在我的jquery datepicker中设置最短日期为(1999-10-25),所以我试着下面的代码不工作。在jquery datepicker中设置最短日期

$(function() { 
    $('#datepicker').datepicker({ 
     dateFormat: 'yy-mm-dd', 
     showButtonPanel: true, 
     changeMonth: true, 
     changeYear: true, 
     showOn: "button", 
     buttonImage: "images/calendar.gif", 
     buttonImageOnly: true, 
     minDate: new Date(1999, 10 - 1, 25), 
     maxDate: '+30Y', 
     inline: true 
    }); 
}); 

**,如果我最小年份改为2002年以上的非它会正常工作,但如果我指定最低年收入低于2002 {像上面1999年eexample}这将只显示多达2002.can帮助我的人。我使用jquery-1.7.1.min.js和jquery-ui-1.8.18.custom.min.js。

回答

79
$(function() { 
    $('#datepicker').datepicker({ 
     dateFormat: 'yy-mm-dd', 
     showButtonPanel: true, 
     changeMonth: true, 
     changeYear: true, 
yearRange: '1999:2012', 
     showOn: "button", 
     buttonImage: "images/calendar.gif", 
     buttonImageOnly: true, 
     minDate: new Date(1999, 10 - 1, 25), 
     maxDate: '+30Y', 
     inline: true 
    }); 
}); 

只需添加年份范围选项。它应该解决问题

+0

感谢的人它的工作 – user1184777 2012-04-14 03:53:22

+0

感谢@Gaurv。它的工作正常。 – 2012-12-28 04:58:05

+0

不错,有帮助 – 2016-01-05 07:06:46

1

你好工作演示http://jsfiddle.net/femy8/

现在日历只会去的1999年10月25日最低。

单击文字框旁边的图像即可显示日历。你可以尝试选择,直到1999年,但选择的最短日期是1999年10月25日。这正是你想要的。

这将有助于,有一个很好的! :)干杯!

jQuery代码

$(".mypicker").datepicker({ 
    changeYear: true, 
    dateFormat: 'yy-mm-dd', 
    showButtonPanel: true, 
    changeMonth: true, 
    changeYear: true, 
    showOn: "button", 
     buttonImage: "images/calendar.gif", 
     buttonImageOnly: true, 
     minDate: new Date('1999/10/25'), 
     maxDate: '+30Y', 
     inline: true 
}); 


​ 
13

的问题是, “yearRange” 的默认选项为10年。

因此2012 - 10 = 2002

因此,将yearRange更改为c-20:c或只是1999(yearRange: '1999:c'),并将其与限制日期(mindate,maxdate)结合使用。

欲了解更多信息:http://jqueryui.com/demos/datepicker/#option-yearRange


见例如:http://jsfiddle.net/kGjdL/

而且你的代码中加入:

$(function() { 
    $('#datepicker').datepicker({ 
     dateFormat: 'yy-mm-dd', 
     showButtonPanel: true, 
     changeMonth: true, 
     changeYear: true, 
     showOn: "button", 
     buttonImage: "images/calendar.gif", 
     buttonImageOnly: true, 
     minDate: new Date(1999, 10 - 1, 25), 
     maxDate: '+30Y', 
     yearRange: '1999:c', 
     inline: true 
    }); 
}); 
+0

良好的简要说明 – sockmonk 2013-05-13 20:26:40

0

只是想添加这只是为未来的程序员。

此代码限制日期的最小值和最大值。 年度完全受当年最大年份的控制。

希望这可以帮助任何人。

这是代码。

var dateToday = new Date(); var yrRange ='2014'+“:”+(dateToday。和getFullYear());

$(function() { 
    $("[id$=txtDate]").datepicker({ 
     showOn: 'button', 
     changeMonth: true, 
     changeYear: true, 
     showButtonPanel: true, 
     buttonImageOnly: true, 
     yearRange: yrRange, 
     buttonImage: 'calendar3.png', 
     buttonImageOnly: true, 
     minDate: new Date(2014,1-1,1), 
     maxDate: '+50Y', 
     inline:true 
    }); 
}); 
0

尝试这样

<script> 

    $(document).ready(function(){ 
      $("#order_ship_date").datepicker({ 
      changeMonth:true, 
      changeYear:true,   
      dateFormat:"yy-mm-dd", 
      minDate: +2, 
     }); 
    }); 


</script> 

下面的HTML代码

<input id="order_ship_date" type="text" class="input" style="width:80px;" /> 
+1

您能否详细说明您的答案,并添加关于您提供的解决方案的更多描述? – abarisone 2015-04-15 10:18:33

0

基本上给出了如果你已经指定年份范围,没有必要使用mindatemaxdate如果只有一年需要

2

只是万一你需要把一个分日期,最近3个月,最大日起未来3个月

$('#id_your_date').datepicker({ 
    maxDate: '+3m', 
    minDate: '-3m' 
}); 
相关问题