javascript - 设置 jquery Datepicker 的最小/最大日期

标签 javascript jquery jquery-ui datepicker

我无法理解如何设置 jquery Datepicker 的最小/最大日期。当我检查 docs ,我发现 dateformat 应该出现在 new Date(2009, 1 - 1, 26) 中。 1-1 是什么意思?当我检查w3c时js 日期格式学校,我找不到这个。

所以我尝试了

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

但这给我留下了完全随机的结果......它从 2006 年开始到 2026 年结束。

这是 fiddle http://jsfiddle.net/ANF2y/58/

enter image description here

最佳答案

在 JavaScript 中, 的计算范围是 0 到 11。因此,在创建新日期时,请使用当前月份 - 1

1 - 1 等于 0。所以这是一个有效的月份(0 - 一月)。

在您的情况下,设置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. When it is set to 2001 you see 2006 in the dropdown as it is limited to 15 items. Select 2006 and then you'll see 2001 in the dropdown. Is your question can I show more items in the year dropdown list?

关于javascript - 设置 jquery Datepicker 的最小/最大日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39660124/

相关文章:

javascript - Php 中未捕获动态添加的输入值

javascript - 在此代码中添加确认?

javascript - Laravel 通过 javascript 选择项目

Javascript计算不返回文本框

javascript - 通过索引引用对象的属性是否安全?

jquery - 将背景div与原始div一起拖动

javascript - 基于其他自动完成输入的 jQuery 自动完成选项

javascript - AJAX 请求不起作用

PHP jQuery 验证

javascript - 如何查找单词在 div 中的位置(y 坐标)