简体   繁体   中英

Set date of jQuery UI Datepicker in “MM yy” format

I'm trying to use jQuery UI datepicker in my project and it seems that I can't set date at render time in format as "MM yy" . But I still can change it in onClose method after page is loaded. If I change initial date format to "yy-mm-dd" initial date is set correctly. Example is here: http://jsfiddle.net/DBpJe/1446/

In this example if you change dateFormat to "yy-mm-dd" then the date is set correctly to a value of realDate variable. If dateFormat is set to "MM yy" , the date is set to current date.

I appreciate any help.

try setting date at the end instead of in beginning,here is working demo http://jsfiddle.net/DBpJe/1446/

    $(function() {
    var queryDate = '2009-11-01',
    dateParts = queryDate.match(/(\d+)/g)
    realDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);  
                                    // months are 0-based!

    $('#startDate').datepicker({
        dateFormat: "MM yy"
    }) // format to show

    .datepicker("option", "changeMonth", true)
    .datepicker("option", "changeYear", true)
    .datepicker("option", "showButtonPanel", true)
    .datepicker("option", "onClose", function(e){
         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));
      }).datepicker('setDate',realDate);
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM