Monday, June 13, 2016

Get "rd" "th" "rd" from datepicker date #javascript

Problem
I'm using a jquery datepicker and when the user picks a date i want to show in another textbox the "rd" "th" "rd" day of the month.

For example:
6/13/2016

I want to show in another textbox "13th" of the month.

Solution
Found a solution on SO. This saved me quite a bit of typing :)

 function get_nth_suffix(date) {
   switch (date) {
     case 1:
     case 21:
     case 31:
        return 'st';
     case 2:
     case 22:
        return 'nd';
     case 3:
     case 23:
        return 'rd';
     default:
        return 'th';
   }
 }

Ref: http://stackoverflow.com/a/6003589/1538717

No comments:

Post a Comment

There was an error in this gadget