在 jquery 月份选择器中更改日期格式后,为什么 from<to 函数不再起作用

After changing the date format in jquery month picker, why the from<to function not works anymore

我遵循了以下大部分代码:http://techbrij.com/month-range-picker-jquery-ui-datepicker

但是我想更改日期格式,因此我更改了以下代码中的月份格式:http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js

问题是原来的函数总是设置 "from" <"to" 日期在我做了下面提到的 2 个更改后不再工作:

** jquery-ui.min.js **

中的原始一个
monthNames:["January","February","March","April","May","June","July","August","September","October","November","December"],
monthNamesShort:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]

更改为以下内容:并相应地更改文件源

monthNames:["01","02","03","04","05","06","07","08","09","10","11","12"],
monthNamesShort:["01","02","03","04","05","06","07","08","09","10","11","12"]

在html也改

示例:201410,则年=2014,月=10

year = datestr.substr(0,datestr.length-3);
month = datestr.substr(datestr.length-2,datestr.length-1);

完整代码如下:

<script type="text/javascript">
     $(function() {
        $( "#from, #to" ).datepicker({ 
           changeMonth: true,
           changeYear: true,
           showButtonPanel: true,
           dateFormat: 'yyMM', 

           onClose: function(dateText, inst) { 
               var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
               var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();             
               $(this).datepicker('setDate', new Date(year, month, 1));
           },
           beforeShow : function(input, inst) {
               if ((datestr = $(this).val()).length > 0) {
                   year = datestr.substr(0,datestr.length-3);  //here changed
                   month = datestr.substr(datestr.length-2,datestr.length-1);  //here changed

                   $(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
                   $(this).datepicker('setDate', new Date(year, month, 1));    
               }
               var other = this.id == "from" ? "#to" : "#from";
               var option = this.id == "from" ? "maxDate" : "minDate";        
               if ((selectedDate = $(other).val()).length > 0) {
                   year = datestr.substr(0, selectedDate.length-3);      //here changed
                   month = datestr.substr(selectedDate.length-2,selectedDate.length-1);        //here changed
                   $(this).datepicker( "option", option, new Date(year, month, 1));
               }
           }
       });
       $("#btnShow").click(function(){ 
       if ($("#from").val().length == 0 || $("#to").val().length == 0){
           alert('All fields are required');
       }
       else{
           alert('Selected Month Range :'+ $("#from").val() + ' to ' + $("#to").val());
           }
       })

希望有大佬帮忙分析下原因,先谢谢了

已修改

year = datestr.substr(0,datestr.length-2);
month = datestr.substring(datestr.length-2);

由于日期格式是 yyMM,datestr 会 return 类似于 201511。您正在提取

year = datestr.substring(0,3); // 201

少了一个字符。使用

year = datestr.substring(0,4); // 2015