jQuery UI datepicker mm / dd / y格式1915年转换为2015年

时间:2016-12-13 09:55:59

标签: javascript jquery date jquery-ui datepicker

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

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

有关如何解决这个问题的想法吗?

1 个答案:

答案 0 :(得分:1)

我发现了问题和解决方案。

问题在于我使用的代码中的somwhere" 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