在“MM yy”中设置jQuery UI Datepicker的日期格式 [英] Set date of jQuery UI Datepicker in "MM yy" format

查看:149
本文介绍了在“MM yy”中设置jQuery UI Datepicker的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中使用jQuery UI datepicker,并且看起来我无法在渲染时将格式设置为MM yy。但是,在页面加载后,我仍然可以在 onClose 方法中更改它。如果我将初始日期格式更改为yy-mm-dd初始日期设置正确。
示例在这里:
http://jsfiddle.net/DBpJe/1446/

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/

在此示例中,如果您将 dateFormat 更改为yy-mm-dd 然后将日期正确设置为值 realDate 变量。如果 dateFormat 设置为MM yy,则日期设置为当前日期。

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.

我感谢任何帮助。

推荐答案

尝试设置日期到底而不是开始,这里是工作示范 http://jsfiddle.net/DBpJe/1446/

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

这篇关于在“MM yy”中设置jQuery UI Datepicker的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆