Created
July 28, 2011 18:50
-
-
Save thinkt4nk/1112237 to your computer and use it in GitHub Desktop.
Given a day of the month, return a stringified representation of it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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