1. 01. Incoming
  2. 02. Hifi
  3. 03. Lofi
  4. 04. The Lab
  5. 05. Tags

frequency decoder

Correctly calculating a date suffix

Posted Thursday July 20, 2006

frequency decoder

A one line script that (correctly) calculates an English language number or date suffix

The simple one-line solution

I recently searched for a JavaScript that could dynamically calculate an English language number suffix i.e. “st”, “nd”, “rd” or “th”; but was astounded by how many of the available scripts either returned incorrect results or, when a correct result was returned, used an abundance of code to calculate the suffix.

So, for anyone searching for a similar script, here’s a quick function that should do the trick:

    
function daySuffix(d) {
    d = String(d);
    return d.substr(-(Math.min(d.length, 2))) > 3 && d.substr(-(Math.min(d.length, 2))) < 21 ? "th" : ["th", "st", "nd", "rd", "th"][Math.min(Number(d)%10, 4)];
}
    
  

Just pass a number to the function and it will return the correct English language suffix e.g. calling daySuffix(113) will return “th”, calling daySuffix(3) will return “rd”.

A demo of the function in action is now available.

Tags: javascript

Previous Comments ~

Ice KAPOM
#1 · Thursday July 27, 2006

hey geek !! how is it doing ????
please be free on the 9th of september. It’s my 30 and Id like to see you both (swiming pool, Dj’s etc)

(by the way, I do not understand function daySuffix(d) {

return d.substr(-(Math.min(d.length, 2))) > 3 && d.substr(-
(Math.min(d.length, 2))) < 21 ? “th” : [“st”, “nd”, “rd”,
“th”][Math.min(d%10, 4)-1];
frequency decoder
#2 · Thursday July 27, 2006

@ Ice: Hey man, good to hear from you… of course I’ll be free on the 9th (as long as it’s not you on the turntables). As for being a “geek”... erm… I most probably am.

P.S. Couldn’t you just have sent me an email?

P.P.S. You English language skills are getting worse with each comment.

See ya soon,
Brian.

Richard · http://www.vfme.com
#3 · Thursday August 24, 2006

Ummm – Hi Brian.
I’m returning “rd” for 113 which I don’t think is quite correct.
Also, you’ve left of the closing bracket of the function…

Cheers,
Richard :o)

frequency decoder
#4 · Thursday August 24, 2006

@ Richard: Hi Richard, well spotted… I forgot to cast the input to a String. The following seems to work:

function daySuffix(d) {

d = String(d); return d.substr(-(Math.min(d.length, 2))) > 3 && d.substr(-(Math.min(d.length, 2))) < 21 ? “th” : [“st”, “nd”, “rd”, “th”][Math.min(d%10, 4)-1];
}
alert(daySuffix(113));
alert(daySuffix(“113”));

Thanks for the comment.

Regards,
Brian.

Mark · http://www.voiceingov.org
#5 · Monday September 11, 2006

Your example returns “undefined” for numbers ending in zero that are larger than 20 (i.e., 30, 40, 50, etc.). Oddly, when I create an array of suffixes using an array constructor, it works fine.

function getSuffix(d) {

d = String(d);

var suffix = new Array();
suffix1] = “th”;
suffix0 = “st”;
suffix1 = “nd”;
suffix2 = “rd”;
suffix3 = “th”;

var result = d.substr((Math.min(d.length, 2))) > 3 && d.substr(-(Math.min(d.length, 2))) < 21 ? “th” : suffix[Math.min(d%10, 4)-1];
return result;

}

Mark · http://www.voiceingov.org
#6 · Monday September 11, 2006

Sorry, the array in that last comment should have looked thusly:

var suffix = new Array();
suffix[-1] = “th”;
suffix0 = “st”;
suffix1 = “nd”;
suffix2 = “rd”;
suffix3 = “th”;

Mark

frequency decoder
#7 · Tuesday September 12, 2006

@ Mark: This is getting embarrasing now… I’ve posted an update that fix’s the problem.

Thanks for the warning!

Regards,
Brian.

Comments are currently closed for this article but feel free to email me with your input - I’d love to hear it.

Popular Frequencies

  • Unobtrusive Table Sort Script (revi…
    Saturday September 16, 2006
  • Unobtrusive Date-Picker Widget Upda…
    Monday October 02, 2006
  • Unobtrusive Table Sort Script
    Friday November 18, 2005
  • Unobtrusive Date-Picker Widgit
    Friday October 14, 2005
  • Unobtrusive Table Actions Script
    Thursday November 15, 2007

Google Ads

All articles