javascript 日期选择器上的两位数年份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13989591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
two-digit year on datepicker
提问by Nello Sorrentino
How is this possible to select a two-digit year in ui-datepicker? When I try to select 24 year or 79 year It returns 1924 or 1979. How can I select the two-digit correctly?
这怎么可能在 ui-datepicker 中选择一个两位数的年份?当我尝试选择 24 年或 79 年时,它返回 1924 或 1979。如何正确选择两位数?
Thanks.
谢谢。
回答by Tomek
Provide date format argument: .datepicker({ dateFormat: 'dd-mm-y' })
(2 digit yeay = y, adjust rest of the format to your need)
提供日期格式参数:.datepicker({ dateFormat: 'dd-mm-y' })
(2 位 yeay = y,根据需要调整其余格式)
回答by raina77ow
The Datepicker plugin has one specific option for such cases - shortYearCutoff:
Datepicker 插件为此类情况提供了一个特定选项 - shortYearCutoff:
shortYearCutoff
The cutoff year for determining the century for a date (used in conjunction with
'y'
dateFormat). Any dates entered with a year value less than or equal to the cutoff year are considered to be in the current century, while those greater than it are deemed to be in the previous century.
短期截止
确定日期世纪的截止年份(与
'y'
dateFormat结合使用)。任何输入的年份值小于或等于截止年份的日期都被认为是在当前世纪,而那些大于它的日期被认为是在上一个世纪。
Its default value is '+10' (ten years from the current one, so in 2012, '22' filled in will be transformed into '2022', but '23' ends up as '1923').
它的默认值是'+10'(从当前的十年算起,所以在2012年,填写的'22'会转化为'2022',但'23'最终会变成'1923')。
You can supply 99 (max value) when initializing your datepicker, like this:
您可以在初始化日期选择器时提供 99(最大值),如下所示:
$( ".selector" ).datepicker({ shortYearCutoff: 99 });
... to make all the two-number years belong to the current century.
...使所有两个数字的年份都属于当前世纪。
回答by Adrian Salazar
Datepicker is not very intuitive in this case.
在这种情况下,Datepicker 不是很直观。
Try this documentation:
试试这个文档:
http://api.jqueryui.com/1.8/datepicker/#option-dateFormat
http://api.jqueryui.com/1.8/datepicker/#option-dateFormat
Set the year-part of the date format to be only y instead of yy
将日期格式的年份部分设置为仅 y 而不是 yy
$(selector).datepicker({ dateFormat: 'dd.mm.y' })
回答by Drew
Adding the shortYearCutoff option did not do anything when the picker window is only activated via button (showOn: 'button'), a user manually enters a date with a 2-digit year, and a 4-digit year is desired.
添加ShortYearcutoff选项在拾取器窗口仅通过按钮激活时没有做任何事情(Showon:'按钮'),用户手动输入具有2位数年份的日期,需要4位数。
I ended up attaching my own change event to the input field to handle date formatting, example code shown below.
我最终将自己的更改事件附加到输入字段以处理日期格式,示例代码如下所示。
It should also be noted that jquery validate is used and configured on the form that this input resides. The validation settings on the datepicker input are configured to enforce a date value. datepicker:{date: true} This combined with the datepicker dateFormat setting configured as "mm/dd/yy" ensure the custom onchange event code handling will reliably work.
还应注意,在此输入所在的表单上使用和配置了 jquery 验证。日期选择器输入上的验证设置被配置为强制执行日期值。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'
});
//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)
回答by venom
doesn't work with the 2 digits from the 2000's like '09' becomes '9' then the year registrated is '209'
不适用于 2000 年代的 2 位数字,例如“09”变为“9”,则注册年份为“209”
this code handles it :
这段代码处理它:
// 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) {
if (last2 < 10) {
d = d + '200' + last2;
} else {
d = d + '20' + last2;
}
} else {
if (last2 < 10) {
d = d + '190' + last2;
} else {
d = d + '19' + last2;
}
}
}
回答by Chris Rehfeldt
Modified code above to basicly scale based on the current date to use the year that is closest to the current year and handle 1 digit years and ignore 3 digits for the year:
修改上面的代码以基本上根据当前日期进行缩放以使用最接近当前年份的年份并处理 1 位数年份并忽略年份的 3 位数:
.on("change",function(){
var d = $(this).val();
var last3 = d.substr(-3);
// determine if the last 3 characters contain the character: /
if(last3.indexOf('/') > -1){
var currentYearStr=(new Date()).getFullYear().toString();
var currentCentury=(currentYearStr.substr(0,2)*1);
var last2 = (d.substr(-2));
// determine if the last 2 characters contain the character: /
if(last2.indexOf('/') > -1) {
// determine which century, within 50 years of the current year, to prepend and prepend that and decade 0 to the 1 digit year:
var last = (d.substr(-1)*1);
var yeardiff=(currentYearStr.substr(-2)*1)-last;
if (yeardiff>50) currentCentury++;
d = d.slice(0,(d.length-1));
d = d+currentCentury.toString()+'0'+last;
} else {
// determine which century, within 50 years of the current year, to prepend and prepend that to the 2-digit year:
last2=last2*1;
var yeardiff=(currentYearStr.substr(-2)*1)-last2;
if (yeardiff>50) currentCentury++;
else if (yeardiff<-50) currentCentury--;
d = d.slice(0,(d.length-2));
d = d+currentCentury.toString()+last2;
}
// store/display the properly formated date
d = $.datepicker.parseDate('mm/dd/yy', d);
$(this).datepicker('setDate', d);
}
});
回答by kidwon
Put that in your datepicker initialization
把它放在你的日期选择器初始化中
"dateFormat": "yy-mm-dd"