Ordinal (st, nd, rd, th) dates in javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph Numpty

    #1

    Ordinal (st, nd, rd, th) dates in javascript

    Hello everyone.

    My first post... I'd like to add an automatically updating date field
    to a webpage. I found the below example on the internet which works
    brilliantly except I'd like an ordinal date (e.g. 1st, 2nd, 3rd, 4th)
    instead of a cardinal date (e.g. 1, 2, 3, 4).

    How could I do that with this small bit of javascript code?

    <script language = "JavaScript ">
    var now = new Date();
    var monNames = new
    Array("January" ,"February","Ma rch","April","M ay","June","Jul y","August","Se ptember","Octob er","November", "December") ;
    document.write( "Last updated: " + now.getDate() + " " +
    monNames[now.getMonth()] + " " + now.getFullYear ());
    </script>

    Thanks in advance for any advice and stuff.

  • Ivo

    #2
    Re: Ordinal (st, nd, rd, th) dates in javascript

    "Joseph Numpty" wrote
    [color=blue]
    > My first post... I'd like to add an automatically updating date field
    > to a webpage. I found the below example on the internet which works
    > brilliantly except I'd like an ordinal date (e.g. 1st, 2nd, 3rd, 4th)
    > instead of a cardinal date (e.g. 1, 2, 3, 4).
    >
    > How could I do that with this small bit of javascript code?
    >
    > <script language = "JavaScript ">[/color]

    Drop the language. Use type instead. Like so:
    <script type="text/javascript">
    [color=blue]
    > var now = new Date();[/color]

    That is the current date, not the last modified date.
    [color=blue]
    > var monNames = new
    >[/color]
    Array("January" ,"February","Ma rch","April","M ay","June","Jul y","August","Se p
    tember","Octobe r","November"," December");[color=blue]
    > document.write( "Last updated: " + now.getDate() + " " +[/color]

    Make that:
    document.write( "Last updated: " + ordi(now.getDat e()) + etc.

    and have a function like so:
    function ordi(n){
    var s='th';
    if(n===1) s='st';
    if(n===2) s='nd';
    if(n===3) s='rd';
    return n+s;
    }
    [color=blue]
    > monNames[now.getMonth()] + " " + now.getFullYear ());
    > </script>[/color]

    For example.
    --Iv


    Comment

    • Joseph Numpty

      #3
      Re: Ordinal (st, nd, rd, th) dates in javascript

      >On Thu, 9 Sep 2004 11:23:31 +0200, "Ivo" <no@thank.you > wrote:[color=blue]
      >[/color]
      <snip>[color=blue]
      >
      >For example.
      >--Iv[/color]

      Thanks very much for your help. You've solved my problem brilliantly.

      Joe

      Comment

      • Philip Ronan

        #4
        Re: Ordinal (st, nd, rd, th) dates in javascript

        On 9/9/04 10:23 am, Ivo wrote:
        [color=blue]
        > and have a function like so:
        > function ordi(n){
        > var s='th';
        > if(n===1) s='st';
        > if(n===2) s='nd';
        > if(n===3) s='rd';
        > return n+s;
        > }[/color]

        This would be better:

        function ordi(n){
        var s='th';
        if(n===1 || n==21 || n==31) s='st';
        if(n===2 || n==22) s='nd';
        if(n===3 || n==23) s='rd';
        return n+s;
        }

        --
        Philip Ronan
        phil.ronanzzz@v irgin.net
        (Please remove the "z"s if replying by email)


        Comment

        • Joseph Numpty

          #5
          Re: Ordinal (st, nd, rd, th) dates in javascript

          >On Thu, 09 Sep 2004 10:50:36 +0100, Philip Ronan <phil.ronanzzz@ virgin.net> wrote:[color=blue]
          >
          >This would be better:
          >
          >function ordi(n){
          > var s='th';
          > if(n===1 || n==21 || n==31) s='st';
          > if(n===2 || n==22) s='nd';
          > if(n===3 || n==23) s='rd';
          > return n+s;
          >}[/color]

          Thanks for that Philip. I added in the early 20's but forgot 31. D'oh!

          Joe

          Comment

          • mehdi amini

            #6
            Re: Ordinal (st, nd, rd, th) dates in javascript


            "Ivo" wrote

            .............[color=blue]
            > and have a function like so:
            > function ordi(n){
            > var s='th';
            > if(n===1) s='st';
            > if(n===2) s='nd';
            > if(n===3) s='rd';
            > return n+s;
            > }[/color]
            ............... .......
            or

            function ordi(n){
            return n+(arguments.ca llee.map[n]||"th");
            }
            ordi.map={1:'st ',21:'st',31:'s t',2:'nd',22:'n d',3:'rd',23:'r d'};

            avoiding all the if's

            bye

            m


            Comment

            • Richard Cornford

              #7
              Re: Ordinal (st, nd, rd, th) dates in javascript

              Philip Ronan wrote:[color=blue]
              > On 9/9/04 10:23 am, Ivo wrote:
              >[color=green]
              >> and have a function like so:
              >> function ordi(n){
              >> var s='th';
              >> if(n===1) s='st';
              >> if(n===2) s='nd';
              >> if(n===3) s='rd';
              >> return n+s;
              >> }[/color]
              >
              > This would be better:
              >
              > function ordi(n){
              > var s='th';
              > if(n===1 || n==21 || n==31) s='st';
              > if(n===2 || n==22) s='nd';
              > if(n===3 || n==23) s='rd';
              > return n+s;
              > }[/color]

              or:-

              function ordi(n){
              var s;
              switch(n%10){
              case 1:
              s = 'st';
              break;
              case 2:
              s = 'nd';
              break;
              case 3:
              s = 'rd';
              break;
              default:
              s = 'th';
              break;
              }
              return n+s;
              }

              Richard.


              Comment

              • Henrik

                #8
                Re: Ordinal (st, nd, rd, th) dates in javascript

                Richard Cornford skrev:
                [color=blue]
                > or:-
                >
                > function ordi(n){
                > var s;
                > switch(n%10){
                > case 1:
                > s = 'st';
                > break;
                > case 2:
                > s = 'nd';
                > break;
                > case 3:
                > s = 'rd';
                > break;
                > default:
                > s = 'th';
                > break;
                > }
                > return n+s;
                > }
                >
                > Richard.[/color]

                This is written a few days before the 11st September.

                --
                Henrik

                Comment

                • Mick White

                  #9
                  Re: Ordinal (st, nd, rd, th) dates in javascript

                  Richard Cornford wrote:
                  [color=blue]
                  > Philip Ronan wrote:[/color]

                  <snip>[color=blue]
                  > function ordi(n){
                  > var s;
                  > switch(n%10){
                  > case 1:
                  > s = 'st';
                  > break;[/color]

                  What about "11"?
                  [color=blue]
                  > case 2:
                  > s = 'nd';
                  > break;[/color]

                  What about "12"?
                  [color=blue]
                  > case 3:
                  > s = 'rd';
                  > break;[/color]

                  What about "13"?
                  [color=blue]
                  > default:
                  > s = 'th';
                  > break;
                  > }
                  > return n+s;
                  > }
                  >
                  > Richard.
                  >[/color]
                  Mick

                  Comment

                  • Fox

                    #10
                    Re: Ordinal (st, nd, rd, th) dates in javascript



                    Joseph Numpty wrote:[color=blue]
                    > Hello everyone.
                    >
                    > My first post... I'd like to add an automatically updating date field
                    > to a webpage. I found the below example on the internet which works
                    > brilliantly except I'd like an ordinal date (e.g. 1st, 2nd, 3rd, 4th)
                    > instead of a cardinal date (e.g. 1, 2, 3, 4).
                    >
                    > How could I do that with this small bit of javascript code?
                    >
                    > <script language = "JavaScript ">
                    > var now = new Date();
                    > var monNames = new
                    > Array("January" ,"February","Ma rch","April","M ay","June","Jul y","August","Se ptember","Octob er","November", "December") ;
                    > document.write( "Last updated: " + now.getDate() + " " +
                    > monNames[now.getMonth()] + " " + now.getFullYear ());
                    > </script>
                    >
                    > Thanks in advance for any advice and stuff.
                    >[/color]

                    handles 0th, and numbers >= 100 (e.g.: 101st) -- in other words, a
                    general purpose method.

                    Number.prototyp e.toOrdinal = function()
                    {
                    var n = this % 100;
                    var suff = ["th", "st", "nd", "rd", "th"]; // suff for suffix
                    var ord= n<21?(n<4 ? suff[n]:suff[0]): (n%10>4 ? suff[0] : suff[n%10]);
                    return this + ord;
                    }

                    use:

                    num.toOrdinal() ;

                    examples:

                    (111).toOrdinal () => 111th
                    (2004).toOrdina l() + " year of the Christian era"...
                    etc...

                    does not test for negatives (but programmers are careful about these
                    things, right?)


                    Comment

                    • Dr John Stockton

                      #11
                      Re: Ordinal (st, nd, rd, th) dates in javascript

                      JRS: In article <ps60k0pu9doje1 608lgj9edr1ssd3 s0u8s@4ax.com>, dated
                      Thu, 9 Sep 2004 10:05:18, seen in news:comp.lang. javascript, Joseph
                      Numpty <joe.numpty@tis cali.co.uk> posted :
                      [color=blue]
                      >My first post... I'd like to add an automatically updating date field
                      >to a webpage. I found the below example on the internet which works
                      >brilliantly except I'd like an ordinal date (e.g. 1st, 2nd, 3rd, 4th)
                      >instead of a cardinal date (e.g. 1, 2, 3, 4).
                      >
                      >How could I do that with this small bit of javascript code?
                      >
                      ><script language = "JavaScript ">
                      >var now = new Date();
                      >var monNames = new
                      >Array("January ","February","M arch","April"," May","June","Ju ly","August","S eptemb
                      >er","October", "November","Dec ember");
                      >document.write ("Last updated: " + now.getDate() + " " +
                      >monNames[now.getMonth()] + " " + now.getFullYear ());
                      ></script>
                      >
                      >Thanks in advance for any advice and stuff.[/color]

                      Read the FAQ, and, with its aid, find
                      <URL:http://www.merlyn.demo n.co.uk/js-demos.htm#Suff>
                      for a variety of methods.

                      There is a link in <URL:http://www.merlyn.demo n.co.uk/js-date9.htm>.


                      You should realise that :-
                      A. Most users already know the date
                      B. And/or don't care
                      C. And can easily find out independently of their computer
                      D. But their computer is very probably already showing it
                      E. You show the computer's date, which may be wrong.
                      F. You show the current date, not the last modified date
                      G. Your method does not update the displayed page at midnight
                      H. For lastModified, see my js-date2.htm#lM

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                      Comment

                      • rh

                        #12
                        Re: Ordinal (st, nd, rd, th) dates in javascript

                        "mehdi amini" wrote:
                        [color=blue]
                        > ............... ......
                        > or
                        >
                        > function ordi(n){
                        > return n+(arguments.ca llee.map[n]||"th");
                        > }
                        > ordi.map={1:'st ',21:'st',31:'s t',2:'nd',22:'n d',3:'rd',23:'r d'};
                        >
                        > avoiding all the if's
                        >[/color]

                        or, by going a bit beyond the OP's request and creating a general utility:

                        function ord(n) {
                        var sfx = ["th","st","nd", "rd"];
                        var val = n%100;
                        return n + (sfx[(val-20)%10] || sfx[val] || sfx[0]);
                        }


                        ../rh

                        Comment

                        • Richard Cornford

                          #13
                          Re: Ordinal (st, nd, rd, th) dates in javascript

                          Mick White wrote:[color=blue]
                          > Richard Cornford wrote:[/color]
                          <snip>[color=blue][color=green]
                          >> function ordi(n){
                          >> var s;
                          >> switch(n%10){
                          >> case 1:
                          >> s = 'st';
                          >> break;[/color]
                          >
                          > What about "11"?[/color]
                          <snip>

                          Yes, that is not good at all. I should have just stuck with suggestion
                          changing the successive - if - statements to - if/else - as they would
                          have been mutually exclusive.

                          Richard.


                          Comment

                          Working...