简体   繁体   English

设置jQuery Datepicker的最小/最大日期

[英]Set min/max Date of jquery Datepicker

I am having trouble understanding how to set the min/max Date of the jquery Datepicker. 我在理解如何设置jquery Datepicker的最小/最大日期方面遇到麻烦。 When I check the docs , i see that the dateformat is expected in new Date(2009, 1 - 1, 26) . 当我检查文档时 ,我看到dateformat应该在new Date(2009, 1 - 1, 26) dateformat new Date(2009, 1 - 1, 26) What does the 1-1 mean? 1-1是什么意思? When I chec w3c schools for js dateformats, I cannot find this one. 当我为w3c学校选择js dateformats时,找不到此格式。

So I tried 所以我尝试了

$("#datepicker").datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: 'yyyy-mm',
    minDate: new Date(2001-01),
    maxDate: '+15Y'
});

But that leaves me with completely random results... It starts 2006 and ends 2026. 但这给了我完全随机的结果...它开始于2006年,结束于2026年。

Here is fiddle http://jsfiddle.net/ANF2y/58/ 这是小提琴http://jsfiddle.net/ANF2y/58/

在此处输入图片说明

In JavaScript month is calculated from 0 to 11. So while creating a new Date use current month - 1 . 在JavaScript中, month是从0到11计算的。因此,在创建新Date时,请使用current month-1

1 - 1 equates to 0 . 1 - 1等于0 So that is a valid month ( 0 - January ). 因此,这是一个有效的月份( 0 - January )。

In your case set minDate as below. 在您的情况下,将minDate设置如下。

$("#datepicker").datepicker({
  changeMonth: true,
  changeYear: true,
  minDate: new Date(2001, 0, 1),
  maxDate: '+15Y',
  dateFormat: 'yyyy-mm'
});

that is because the jquery date picker only shows 15 items at a time, there is no max date set so you can go as far in the future as you want. 这是因为jquery日期选择器一次只显示15个项目,没有设置最大日期,因此您可以在将来尽可能远地进行。 When it is set to 2001 you see 2006 in the dropdown as it is limited to 15 items. 如果将其设置为2001,则在下拉列表中会看到2006,因为它仅限于15个项目。 Select 2006 and then you'll see 2001 in the dropdown. 选择2006,然后在下拉列表中会看到2001。 Is your question can I show more items in the year dropdown list? 您是否可以在年份下拉列表中显示更多项目?

I'm not going to go into minDate or maxDate as that has been well covered. 我不会讨论minDate或maxDate,因为已经很好地介绍了它。 But in addition to that, what you are looking for is yearRange will set the number of visible items in the year dropdown of the jquery date picker. 但除此之外,您正在寻找的是yearRange将设置jquery日期选择器的year下拉列表中的可见项数。

The documentation states: 该文档指出:

The range of years displayed in the year drop-down: either relative to today's year ("-nn:+nn"), relative to the currently selected year ("c-nn:c+nn"), absolute ("nnnn:nnnn"), or combinations of these formats ("nnnn:-nn"). 在年份下拉列表中显示的年份范围:相对于今天的年份(“ -nn:+ nn”),相对于当前选定的年份(“ c-nn:c + nn”),绝对值(“ nnnn: nnnn”)或这些格式的组合(“ nnnn:-nn”)。 Note that this option only affects what appears in the drop-down. 请注意,此选项仅影响下拉菜单中显示的内容。

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM