datepicker上的两位数年份

如何在ui-datepicker中选择两位数的年份呢? 当我尝试选择24年或79年时它返回1924年或1979年。如何正确选择两位数?

谢谢。

提供日期格式参数: .datepicker({ dateFormat: 'dd-mm-y' }) (2位数yeay = y,根据需要调整其余格式)

Datepicker插件有针对此类情况的一个特定选项 – shortYearCutoff :

shortYearCutoff

确定日期世纪的截止年份(与'y' y’dateFormat一起使用)。 输入年份值小于或等于截止年份的任何日期都被认为是在本世纪,而那些大于它的日期被认为是在上个世纪。

它的默认值是’+10’(距现在的十年,所以在2012年,’22’填充将转换为’2022’,但’23’结束为’1923’)。

初始化日期选择器时,您可以提供99(最大值),如下所示:

 $( ".selector" ).datepicker({ shortYearCutoff: 99 }); 

……使所有两个数字年都属于本世纪。

在这种情况下,Datepicker不是很直观。

试试这个文档:

http://api.jqueryui.com/1.8/datepicker/#option-dateFormat

将日期格式的年份部分设置为仅y而不是yy

 $(selector).datepicker({ dateFormat: 'dd.mm.y' }) 

http://docs.jquery.com/UI/Datepicker/formatDate

当仅通过按钮(showOn:’button’)激活选择器窗口时,添加shortYearCutoff选项不会执行任何操作,用户手动输入具有2位数年份的日期,并且需要4位数年份。

我最终将自己的更改事件附加到输入字段以处理日期格式,示例代码如下所示。

还应注意,在此输入所驻留的表单上使用并配置了jquery validate。 datepicker输入上的validation设置配置为强制执行日期值。 datepicker:{date:true}这与配置为“mm / dd / yy”的datepicker dateFormat设置相结合,确保自定义onchange事件代码处理可靠地工作。

//datepicker $( "#datepicker" ).attr("placeholder", "mm/dd/yyyy").change(function(){ // get the date value from the input field var d = $(this).val(); // get the last 4 characters of the date value entered var last4 = d.substr(-4); // determine if the last 4 characters contain the character: / if(last4.indexOf('/') > -1){ // yes there is a / character which means a two digit year was provided // store the last two digits from the input string var last2 = ((d.substr(-2))*1); // remove the last two digits from the input string d = d.slice(0,(d.length-2)); // prepend a 19 or 20 to the two digit year given based on a cutOff of 30 if(last2<=30){ d = d+'20'+last2; }else{ d = d+'19'+last2; } } // store/display the properly formated date d = $.datepicker.parseDate('mm/dd/yy', d); $(this).datepicker('setDate', d); }).datepicker({ dateFormat: "mm/dd/yy", shortYearCutoff: 30, buttonImage: 'images/calendar_icon.png', showOn: 'button'
});

 Input values | onchange output results -------------------------------------- 07/04/2000 | 07/04/2000 (no change) 7/4/2000 | 07/04/2000 07/04/00 | 07/04/2000 07/4/00 | 07/04/2000 7/4/00 | 07/04/2000 7/4/30 | 07/04/2030 7/4/31 | 07/04/1931 07/04/2031 | 07/04/2031 (no change) 07/04/1931 | 07/04/1931 (no change) 

把它放在你的日期选择器初始化中

 "dateFormat": "yy-mm-dd"