Adding “st, nd, rd, th” to jquery datepicker

后端 未结 8 502
臣服心动
臣服心动 2020-12-11 15:44

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

Currently, I have following code

    $( \"#datepicker\" ).datepi         


        
相关标签:
8条回答
  • 2020-12-11 16:39
        // Get the ordinal suffix for the given int value
    var ordinalSuffix = function (val) {
    var mod = val % 10;
    if (mod === 1 && val !== 11) {
    return 'st';
    } else if (mod === 2 && val !== 12) {
    return 'nd';
    } else if (mod === 3 && val !== 13) {
    return 'rd';
    } else {
    return 'th';
    }
    };
    
    0 讨论(0)
  • 2020-12-11 16:41

    Since formatDate() doesn't have that option.

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

    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.

    0 讨论(0)
提交回复
热议问题