【问题标题】:two-digit year on datepicker日期选择器上的两位数年份
【发布时间】:2012-12-09 00:02:02
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: javascript jquery datepicker uidatepicker


    【解决方案1】:

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

    【讨论】:

    • 如果我提供此日期格式,我在 3 位数年份或 4 位数年份或 1 位数年份有问题....我提供了这个:yearRange:"0000:"+ new Date().getFullYear()} 但是当我选择 55year 它在输入中返回 1955 :/
    • 什么是 1 位数年份,我想知道吗? yearRange 与它没有任何关系。
    • 你想做什么?以某种格式检索日期?设置日期?设置显示范围?可以选择的限制范围?
    • 天啊,我到处寻找这个答案。我没有想到我的格式字符串错误。
    【解决方案2】:

    Datepicker 插件针对这种情况有一个特定选项 - shortYearCutoff:

    shortYearCutoff

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

    它的默认值是'+10'(距离现在的十年,所以在2012年,填写的'22'会被转换成'2022',而'23'最终会变成'1923')。

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

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

    ...使所有两位数的年份都属于当前世纪。

    【讨论】:

      【解决方案3】:

      在这种情况下,日期选择器不是很直观。

      试试这个文档:

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

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

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

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

      【讨论】:

        【解决方案4】:

        添加shortyearcutoff选项在拾取器窗口仅通过按钮激活时没有执行任何操作(Showon:'按钮'),用户手动进入具有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)
        

        【讨论】:

          【解决方案5】:

          不适用于 2000 年的 2 位数字,例如“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;
              }
            }
          }
          

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:

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

              "dateFormat": "yy-mm-dd"
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2020-02-07
                • 1970-01-01
                • 1970-01-01
                • 2015-06-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多