添加“st,nd,rd,th"到 jquery 日期选择器

问题描述:

如何将 st,nd,rd,th 添加到日期格式.. 即 11 月 19 日或 12 月 2 日等

How can I add st,nd,rd,th to date format.. i.e. November 19th, or December 2nd, etc.

目前,我有以下代码

    $( "#datepicker" ).datepicker({
        altField: "#alternate",
        altFormat: "DD, MM d ",
                    minDate: +0
    });

因为 formatDate() 没有那个选项.

Since formatDate() doesn't have that option.

是的,你是对的,抱歉我的回答没有帮助你,我是新来的.我希望这次能有所帮助:)

Yes, you were right, sorry my answer didn't help you, I'm new to SO. I hope it helps this time :)

向 datepicker init 添加一个 onSelect 函数,如下所示:

Add an onSelect function to datepicker init, something like this:

$("#datepicker").datepicker({
    altField: "#alternate",
    altFormat: "DD, MM d",
    minDate: +0,
    onSelect: function(dateText, inst) {
        var suffix = "";
        switch(inst.selectedDay) {
            case '1': case '21': case '31': suffix = 'st'; break;
            case '2': case '22': suffix = 'nd'; break;
            case '3': case '23': suffix = 'rd'; break;
            default: suffix = 'th';
        }

        $("#alternate").val($("#alternate").val() + suffix);
    }
});

每次选择日期时,它都会将后缀添加到备用字段.

It adds the suffix to the alternate field each time you select a date.