Formatting JavaScript Date Suffixes

I have done my due diligence in investigating this and have not yet been successful. Being pretty green with JavaScript, I am looking for help. I want to show the date

NOV2012<br> 2<sup>nd</sup><br> 5:00 PM 

I have everything that works (not my script), except for the ability to get the date suffix to change to st, nd, rd or th depending on the case.

This is what I have:

 <pre> <abbr title="Month"> <script type="text/javascript"> var d=new Date(); var month=new Array(12); month[0]="Jan"; month[1]="Feb"; month[2]="Mar"; month[3]="Apr"; month[4]="May"; month[5]="Jun"; month[6]="Jul"; month[7]="Aug"; month[8]="Sep"; month[9]="Oct"; month[10]="Nov"; month[11]="Dec"; document.write(month[d.getMonth()]); </script></abbr> <script type="text/javascript"> var d = new Date() document.write(d.getDate()) ordinal : function (number) { var d = number % 10; return (~~ (number % 100 / 10) === 1) ? 'th' : (d === 1) ? 'st' : (d === 2) ? 'nd' : (d === 3) ? 'rd' : 'th'; } }); </script> <sup>%</sup> <abbr><script type="text/javascript"> var d = new Date() document.write(d.getFullYear()) </script></abbr> <sub> <script type="text/javascript"> <!-- var currentTime = new Date() var hours = currentTime.getHours() var minutes = currentTime.getMinutes() if (minutes < 10){ minutes = "0" + minutes } document.write(hours + ":" + minutes + " ") if(hours > 11){ document.write("PM") } else { document.write("AM") } //--> </script> </sub> </pre> 

I know the problem is this part:

 <pre> <script type="text/javascript"> var d = new Date() document.write(d.getDate()) ordinal : function (number) { var d = number % 10; return (~~ (number % 100 / 10) === 1) ? 'th' : (d === 1) ? 'st' : (d === 2) ? 'nd' : (d === 3) ? 'rd' : 'th'; } }); </script> < sup > % < /sup > </pre> 

but I can not find the right solution. Here he is sitting:

http://www.bcreativeservices.com/

Thanks, as always.

IN

+4
source share
1 answer

First, you have a syntax error assigning a sequence number. It looks like you initially tried to create an object containing the key serial number, but later changed it.

This is probably what you were looking for:

 function ordinal(number) { var d = number % 10; return (~~ (number % 100 / 10) === 1) ? 'th' : (d === 1) ? 'st' : (d === 2) ? 'nd' : (d === 3) ? 'rd' : 'th'; } 

Which works, but double bitwise NOT ~~ makes your code a little difficult to follow. I had to see what it was (I never use beaten math), and I would recommend that you not use it (unless you have reason to use it, of course).

In accordance with this question on this issue you get speed improvements. However, these operations take fractions of a microsecond, so the improvement is really insignificant and only helps to make your code more difficult to understand.


Not so long ago, I wrote a function to provide these suffixes for dates. After a little change (I added my Date prototype), you will get:

 function ordinal(date) { return (date > 20 || date < 10) ? ([false, "st", "nd", "rd"])[(date%10)] || "th" : "th"; } 

What works on any valid date.

EDIT: It seems to me that my version of the ordinal function is probably also impossible to read. For common sense, here is a less compressed version with the same logic:

 function ordinal(date) { if(date > 20 || date < 10) { switch(date%10) { case 1: return "st"; case 2: return "nd"; case 3: return "rd"; } } return "th"; } 
+12
source

Source: https://habr.com/ru/post/1443745/


All Articles