Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding "st, nd, rd, th" to jquery datepicker

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" ).datepicker({
        altField: "#alternate",
        altFormat: "DD, MM d ",
                    minDate: +0
    });
like image 315
dave Avatar asked Nov 20 '11 03:11

dave


2 Answers

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.

like image 171
Renato S. Martins Avatar answered Oct 05 '22 09:10

Renato S. Martins


Chris West has a pretty short and sweet definition for a function which generates ordinals for all integers. On this page he even shows how you can easily use his function to add ordinals to all numbers within a string.

like image 43
Yolanda Vaverek Avatar answered Oct 05 '22 08:10

Yolanda Vaverek