You might start with JavaScript Date/Time Functions to get the Day number:
var theDate = myDateObj.GetDate(); // returns 1-31
Then you will need to write a rule to get the proper suffix. Most of the time it will be th
, except for the exceptions. What are the exceptions? 1, 21, 31 = st
, 2, 22 = nd
, 3, 23 = rd
, everything else is th
. So we can use mod %
to check if it ends in 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 could be boring and use a library :-)