JavaScript date suffix formatting

Posted by TexasB on Stack Overflow See other posts from Stack Overflow or by TexasB
Published on 2012-11-02T21:50:13Z Indexed on 2012/11/02 23:01 UTC
Read the original article Hit count: 184

Filed under:

I have done my due diligence in investigating this and not had any success yet. Being rather green with JavaScript I am seeking some help. I am wanting to display the date

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

I have everything working (not my script) except being able to get the date suffix to change to st, nd, rd, or th as the case may be.

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 issue is with 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't seem to work out the right fix. This is where it is sitting:

http://www.bcreativeservices.com/

Thank you as always.

B

© Stack Overflow or respective owner

Related posts about JavaScript