datepicker上的两位数年份

时间:2012-12-21 11:46:22

标签: javascript jquery datepicker uidatepicker

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

感谢。

7 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:6)

Datepicker插件针对此类情况提供了一个特定选项 - shortYearCutoff

  

<强> shortYearCutoff

     

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

它的默认值是'+10'(距离当前的十年,所以在2012年,'22'填充将转换为'2022',但'23'最终将变为'1923'。

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

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

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

答案 2 :(得分:2)

在这种情况下,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

答案 3 :(得分:1)

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

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

还应该注意,在此输入所在的表单上使用和配置了jquery validate。 datepicker输入上的验证设置配置为强制执行日期值。 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)

答案 4 :(得分:0)

不能与2000年的两位数字一起使用,例如“ 09”变为“ 9”,则注册的年份为“ 209”

此代码可以处理它:

// 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;
    }
  }
}

答案 5 :(得分:0)

上面修改的代码基本上根据当前日期进行缩放,以使用最接近当前年份的年份并处理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);
                    }
                });

答案 6 :(得分:-1)

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

"dateFormat": "yy-mm-dd"