【问题标题】:jQuery UI datepicker mm/dd/y format year 1915 converted to 2015jQuery UI datepicker mm/dd/y 格式年份 1915 转换为 2015
【发布时间】:2017-04-28 08:07:05
【问题描述】:

我的 jquery ui datepicker 中有一个错误。我的格式是: mm/dd/y ,例如年份是:1915。因此,1915 年 2 月 7 日将是:02/07/15。选择此日期并关闭日期选择器后,我可以通过 Firebug Inspect 看到年份保存得很好,它是 1915,但是当我再次打开日期选择器时,选择的年份是 2015 而不是 1915。

我正在使用 jquery-1.9.1 和 jquery-ui-1.11.4。 我也在使用这个库:https://github.com/trentrichardson/jQuery-Timepicker-Addon

知道如何解决这个问题吗?

【问题讨论】:

  • 您能否提供一些示例代码来说明您如何使用 datepicker。如果您使用的是日期对象而不是字符串,这应该没问题。如果您使用的是字符串,则 JS 无法确定这是 1915 年还是 2015 年。

标签: javascript jquery date jquery-ui datepicker


【解决方案1】:

我找到了问题和解决方案。

问题是我在代码中的某个地方使用了“setYear”函数。根据:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear我发现:

For years greater than or equal to 2000, the value returned by getYear() is 100 or greater. For example, if the year is 2026, getYear() returns 126.
For years between and including 1900 and 1999, the value returned by getYear() is between 0 and 99. For example, if the year is 1976, getYear() returns 76.
For years less than 1900, the value returned by getYear() is less than 0. For example, if the year is 1800, getYear() returns -100.

这个网站也提出了解决方案:

To take into account years before and after 2000, you should use getFullYear() instead of getYear() so that the year is specified in full.

感谢您提供帮助。我希望这也将在未来对其他人有所帮助。 不要使用 setYear 函数,使用 setFullYear

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2018-01-13
    • 1970-01-01
    相关资源
    最近更新 更多