You can start with JavaScript Date / Time Functions to get the day number:
var theDate = myDateObj.GetDate();
Then you will need to write a rule to get the correct suffix. In most cases this will be th , with the exception of exceptions. What are the exceptions? 1, 21, 31 = st , 2, 22 = nd , 3, 23 = rd , everything else is th . Therefore, we can use mod % to check if it ends with 1, 2 or 3:
var nth = ''; if (theDate > 3 && theDate < 21) // catch teens, which are all 'th' nth = theDate + 'th'; else if (theDate % 10 == 1) // exceptions ending in '1' nth = theDate + 'st'; else if (theDate % 10 == 2) // exceptions ending in '2' nth = theDate + 'nd'; else if (theDate % 10 == 3) // exceptions ending in '3' nth = theDate + 'rd'; else nth = theDate + 'th'; // everything else
Here's a working demo showing the endings for 1-31: http://jsfiddle.net/6Nhn8/
Or you may be bored and use the library :-)
source share