如何使用jQuery UI日历自定义输出日期格式

时间:2015-03-21 07:00:28

标签: jquery jquery-ui jquery-plugins datepicker

我要求以特定格式显示日期。日期中的月份应仅显示四个字符。即,1月,2月,3月,4月,5月,6月,7月,9月,10月,11月,12月..是否可以使用jQuery UI datepicker以此格式显示月份,保持日期格式?

2 个答案:

答案 0 :(得分:1)

试试这段代码:

$( "#datepicker" ).datepicker( "option", "dateFormat", "dd M. yy" );

<强>演示

$(function () {

    var months = ["January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"];

    $("#datepicker").datepicker({
        monthNamesShort: ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept.", "Oct", "Nov", "Dec"],
        onSelect: function (date) {
            var monthtext = months[$("#datepicker").datepicker('getDate').getMonth()];
            if (monthtext.length == 4) {
                $("#datepicker").datepicker("option", "dateFormat", "dd M yy");
            } else {
                $("#datepicker").datepicker("option", "dateFormat", "dd M. yy");
            }
        }
    });
    $("#datepicker").datepicker("option", "dateFormat", "dd M. yy");

});
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">

<p>Date: <input type="text" id="datepicker" size="30"></p>

答案 1 :(得分:1)

您可以操纵datepicker方法并执行此回调:

$(".selector").datepicker(
 { monthNamesShort: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]}
);