Add ST/ND/RD/TH to DAY in DATE

Show 40 post(s) from this thread on one page
Page 3 of 3 First 123
• 03-26-2010, 02:55 PM
Philip M
I still prefer

Code:

```function numsuffix(n) { return n + ["th","st","nd","rd"][!(n%10>3||Math.floor(n%100/10)==1)*n%10]; }```
• 03-26-2010, 04:02 PM
bardonw
Quote:

Originally Posted by RoyW
I like the boolean trick and I like the idea of only having 4 strings in the array
Code:

```function num_abbrev_str(n) {     return n + ["th","st","nd","rd"][!(n%10>3||Math.floor(n%100/10)==1)*n%10]; }```
In this 60% of the time the
(n%10>3)
will be true so the
Math.floor(n%100/10)==1
will only be executed for 40% of the numbers.

Invert the boolean and do the multiply
NOTE: I have utilised operator precedence to reduce char count.
Here is a version with parentheses to show precedence
PHP Code:

``` function num_abbrev_str(n){    return n + ["th","st","nd","rd"][(!( ((n%10) >3) || (Math.floor(n%100/10)==1)) ) * (n%10)]; }  ```

I must admit, this is some impressive code! :thumbsup: After seeing this I decided to optimize it just a bit by storing the array of suffixes privately. After doing that and modifying it to have the same footprint as my generic function, I ended up with the following code:
Code:

```var num_abbrev_str = (function() {         var z = ["th","st","nd","rd"];         return function(intNum, includeNumber)         {                 return (includeNumber ? intNum : "") + z[!((intNum = Math.abs(intNum))                         % 10 > 3 || Math.floor(intNum % 100 / 10) == 1) * intNum % 10];         }; })();```
The plus side is, the function works very well. The only disadvantage is, on Internet Explorer and Firefox, this function takes longer to run, for a large number of integers, than the code below does. On the other hand, I doubt that too many people will be running this type of function for 100,000+ numbers.
Code:

```function num_abbrev_str(intNum, includeNumber) {         return (includeNumber ? intNum : "") + (((intNum = Math.abs(intNum) % 100)                 % 10 == 1 && intNum != 11) ? "st" : (intNum % 10 == 2 && intNum != 12)                 ? "nd" : (intNum % 10 == 3 && intNum != 13) ? "rd" : "th"); }```
Still, your code is most impressive. :thumbsup:
• 03-26-2010, 04:42 PM
Philip M
In this forum it is often said that there are several good ways to skin a cat, but it is also said that re-inventing the wheel (but making it oval this time) does not add very much. :)
• 03-27-2010, 02:54 AM
RoyW
Looks like I still have email notifications on for this thread. A 7 year old thread! That must be a record :)
• 10-10-2012, 05:05 PM
bardonw
After looking at someone else's implementation, I improved mine and created the smallest version that I could think of with the first parameter being the number and the second being the boolean indicating whether or not to show the number. I also used a closure in order to prevent the array from being recreated every time the function ran (this improves the speed):
Code:

`(function(b){Number.getOrdinalFor=function(a,c){return(c?a:"")+(b[((a=Math.abs(a%100))-20)%10]||b[a]||"th")}})([,"st","nd","rd"]);`
This function is only 130 characters long and will work for all integers. I also came up with a pretty cool example of how to use this for a string full of numbers:
Code:

```var str = "1, 2, 3, 4.5, 6, and 7 are all numbers."; alert(str.replace(/(\d+)/g, Number.getOrdinalFor));```
http://gotochriswest.com/blog/2012/0...getordinalfor/

Let me know if you can write the equivalent function in an even shorter way while also making sure that something like an array doesn't have to be generated every time the function is called. That last requirement is what makes my function a little longer than it seems it needs to be. :cool:
• 10-10-2012, 09:50 PM
AndrewGSW
You could use a string rather than an array, incorporating:

XX = "thstndrd".substr((D % 10) * 2, 2) || "th";

It may not be shorter but avoids "Index out of range" or "undefined".

Oops! This is not quite right?!
• 10-10-2012, 10:02 PM
Philip M
Code:

```<script type="text/javascript"> var D = 12; var XX = "thstndrd".substr((D % 10) * 2, 2) || "th"; alert (XX);  // nd!!! </script>```
:(:(
• 10-10-2012, 10:04 PM
AndrewGSW
Quote:

Originally Posted by Philip M
Code:

```<script type="text/javascript"> var D = 12; var XX = "thstndrd".substr((D % 10) * 2, 2) || "th"; alert (XX);  // nd!!! </script>```
:(:(

Yes, shame though - it looked good! Perhaps we can change the suffixes to match my formula ;).
• 10-10-2012, 10:06 PM
• 10-10-2012, 10:38 PM
AndrewGSW
Code:

`XX = "thstndrd".substr((D % 10) * !(D % 14 > 9) * 2, 2) || "th";`
I'm considering dates though (from the title of this post) so haven't gone beyond 31 :) Added: Yes, works beyond 31.
• 10-11-2012, 03:09 AM
AndrewGSW
Code:

`XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th";`
.. works with non-date numbers.
• 10-11-2012, 08:03 AM
Philip M
Quote:

Originally Posted by AndrewGSW
Code:

`XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th";`
.. works with non-date numbers.

No it doesn't!

Code:

```var D = 112; var XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th"; alert (XX);  // nd!!!```

Code:

```var D = 112; var XX = "thstndrd".substr((D % 10) * !(D % 14 > 9) * 2, 2) || "th"; alert (XX);  // nd!!!```
See Post # 31 - that does work!
• 10-12-2012, 08:49 PM
bardonw
Quote:

Originally Posted by Philip M
No it doesn't!

Code:

```var D = 112; var XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th"; alert (XX);  // nd!!!```

Code:

```var D = 112; var XX = "thstndrd".substr((D % 10) * !(D % 14 > 9) * 2, 2) || "th"; alert (XX);  // nd!!!```
See Post # 31 - that does work!

I think that is a good version and I definitely spent some time looking at how to make it work a bit differently. The only problem I have is the fact that in that version, the array of ordinals is created every time the function is run. That means that the function will run slower if run on many thousands of numbers. Still, for most cases, that function will do. I just know that people like John Resig or Douglas Crockford would not like me endorsing that as the best solution.
• 10-13-2012, 02:55 AM
AndrewGSW
I suppose..

Code:

`var XX = "thstndrd".substr((D % 10) * (D % 100 < 11 || D  % 100 > 13) * 2, 2) || "th";`
but my interest was really in working with days (from the OP title).
• 10-13-2012, 07:49 AM
Philip M
Quote:

Originally Posted by bardonw
The only problem I have is the fact that in that version, the array of ordinals is created every time the function is run. That means that the function will run slower if run on many thousands of numbers. Still, for most cases, that function will do. I just know that people like John Resig or Douglas Crockford would not like me endorsing that as the best solution.

I would doubt if there was any perceptible slowness (milliseconds perhaps) even if run on "many thousand of numbers". And where in reality is that required? Does the need to generate 1st to 123456th ever really arise?
Show 40 post(s) from this thread on one page
Page 3 of 3 First 123