Skip to content

Instantly share code, notes, and snippets.

@thinkt4nk
Created July 28, 2011 18:50
Show Gist options
  • Save thinkt4nk/1112237 to your computer and use it in GitHub Desktop.
Save thinkt4nk/1112237 to your computer and use it in GitHub Desktop.
Given a day of the month, return a stringified representation of it
Array.prototype.contains = function(elem)
{
return (this.indexOf(elem) === -1) ? false : true;
}
function addNumberSuffix(n)
{
var suffix = '';
n = parseInt(n);
var number_last_digit = parseInt((''+n).substr(-1,1));
if( [4,5,6,7,8,9,0].contains(number_last_digit) || (n > 10 && n < 14) )
{
suffix = 'th';
}
else if( number_last_digit === 1 )
{
suffix = 'st';
}
else if( number_last_digit === 2 )
{
suffix = 'nd';
}
else if( number_last_digit === 3 )
{
suffix = 'rd';
}
return '' + n + suffix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment