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

馋奶兔 提交于 2019-12-24 19:10:28

问题


I followed most of the code in:http://techbrij.com/month-range-picker-jquery-ui-datepicker.

But I want to change the date format, therefore I changed the monthformat in the following code:http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js.

The problem is the original function always setting "from" <"to" date is not working anymore after I made 2 changes mentioned as below:

**original one in 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"]

changed to following: and also change the source of file accordingly

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"]

in html also change

Example:201410, then year=2014, month=10

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

you can see the complete code as below:

<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());
           }
       })

Hope someone could help figure out the reason, thanks in advance.

Modified

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

回答1:


Since the dateFormat is yyMM, datestr would return something like 201511. You are extracting

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

Which is missing by one character. Use

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


来源:https://stackoverflow.com/questions/33471397/after-changing-the-date-format-in-jquery-month-picker-why-the-fromto-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!