JavaScript new Date Ordinal (st, nd, rd, th)










30















If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:



13th March 2013



The code I am using is:



var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;


which returns the date and time two weeks from now, but like this:
Wed Mar 27 2013 21:50:29 GMT+0000 (GMT Standard Time)



Here is the code in jsFiddle.



Any help would be appreciated!










share|improve this question
























  • apart from a missing "of" I have no problem with that date

    – mplungjan
    Mar 13 '13 at 22:05











  • try this stackoverflow.com/questions/1056728/…

    – ebram khalil
    Mar 13 '13 at 22:05











  • I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it

    – v0d1ch
    Nov 25 '14 at 13:40















30















If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:



13th March 2013



The code I am using is:



var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;


which returns the date and time two weeks from now, but like this:
Wed Mar 27 2013 21:50:29 GMT+0000 (GMT Standard Time)



Here is the code in jsFiddle.



Any help would be appreciated!










share|improve this question
























  • apart from a missing "of" I have no problem with that date

    – mplungjan
    Mar 13 '13 at 22:05











  • try this stackoverflow.com/questions/1056728/…

    – ebram khalil
    Mar 13 '13 at 22:05











  • I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it

    – v0d1ch
    Nov 25 '14 at 13:40













30












30








30


11






If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:



13th March 2013



The code I am using is:



var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;


which returns the date and time two weeks from now, but like this:
Wed Mar 27 2013 21:50:29 GMT+0000 (GMT Standard Time)



Here is the code in jsFiddle.



Any help would be appreciated!










share|improve this question
















If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:



13th March 2013



The code I am using is:



var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;


which returns the date and time two weeks from now, but like this:
Wed Mar 27 2013 21:50:29 GMT+0000 (GMT Standard Time)



Here is the code in jsFiddle.



Any help would be appreciated!







javascript date ordinal-indicator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 9 '18 at 22:37









dhilt

8,01742142




8,01742142










asked Mar 13 '13 at 22:02









user1635828user1635828

156124




156124












  • apart from a missing "of" I have no problem with that date

    – mplungjan
    Mar 13 '13 at 22:05











  • try this stackoverflow.com/questions/1056728/…

    – ebram khalil
    Mar 13 '13 at 22:05











  • I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it

    – v0d1ch
    Nov 25 '14 at 13:40

















  • apart from a missing "of" I have no problem with that date

    – mplungjan
    Mar 13 '13 at 22:05











  • try this stackoverflow.com/questions/1056728/…

    – ebram khalil
    Mar 13 '13 at 22:05











  • I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it

    – v0d1ch
    Nov 25 '14 at 13:40
















apart from a missing "of" I have no problem with that date

– mplungjan
Mar 13 '13 at 22:05





apart from a missing "of" I have no problem with that date

– mplungjan
Mar 13 '13 at 22:05













try this stackoverflow.com/questions/1056728/…

– ebram khalil
Mar 13 '13 at 22:05





try this stackoverflow.com/questions/1056728/…

– ebram khalil
Mar 13 '13 at 22:05













I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it

– v0d1ch
Nov 25 '14 at 13:40





I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it

– v0d1ch
Nov 25 '14 at 13:40












12 Answers
12






active

oldest

votes


















56














Here:



JSFiddle






var fortnightAway = new Date(+new Date + 12096e5),
date = fortnightAway.getDate(),
month = ["January","February","March","April","May","June","July",
"August","September","October","November","December"][fortnightAway.getMonth()];

function nth(d)
if (d > 3 && d < 21) return 'th';
switch (d % 10)
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";


document.body.innerHTML = date + nth(date) + " " +
month + " " +
fortnightAway.getFullYear();








share|improve this answer




















  • 2





    Good work.... Thanks

    – Elby
    May 27 '14 at 6:03











  • Works perfect... +1 for d>3 && d<21

    – ĆĦΔŇĐŘΔ ŇΔҜҜΔ
    Apr 17 '15 at 10:25











  • For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";

    – krick
    Dec 15 '16 at 22:36












  • That is missing 21,22,23

    – mplungjan
    Dec 16 '16 at 5:12











  • if (d == 0) return ''; ... if used outside of a date context

    – John
    Apr 13 '18 at 14:47



















16














Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.



function getOrdinalNum(n) 
return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21)





share|improve this answer


















  • 1





    tested 1 to 31 and it works correctly

    – johnc
    Oct 22 '17 at 17:34


















3














Lots of formatting answers, so I'll just work on the nth of any integer-



Number.prototype.nth= function()
if(this%1) return this;
var s= this%100;
if(s>3 && s<21) return this+'th';
switch(s%10)
case 1: return this+'st';
case 2: return this+'nd';
case 3: return this+'rd';
default: return this+'th';







share|improve this answer

























  • The if is for teens, so 13 is 13th and not 13rd.

    – kennebec
    Mar 13 '13 at 22:32












  • Why the this%1? Mod 1 is always 0 so the statement will always fail, no?

    – Forage
    Mar 21 '18 at 11:10


















2














I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.



function dateOrdinal(dom) ;


or compact version using conditional operators



function dateOrdinal(d) 2==d?"nd":23==d;


http://jsben.ch/#/DrBpl






share|improve this answer
































    1














    Lots of answers, here's another:



    function addOrd(n) 

    // Return date string two weeks from now (14 days) in
    // format 13th March 2013
    function formatDatePlusTwoWeeks(d)
    var months = ['January','February','March','April','May','June',
    'July','August','September','October','November','December'];

    // Copy date object so don't modify original
    var e = new Date(d);

    // Add two weeks (14 days)
    e.setDate(e.getDate() + 14);
    return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();


    alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013





    share|improve this answer






























      0














      I'm a bit late to the party, but this should work:



      function ordinal(number) 
      number = Number(number)
      if(!number

      function specialFormat(date)
      // add two weeks
      date = new Date(+date + 12096e5)
      var months = [
      'January'
      , 'February'
      , 'March'
      , 'April'
      , 'May'
      , 'June'
      , 'July'
      , 'August'
      , 'September'
      , 'October'
      , 'November'
      , 'December'
      ]
      var formatted = ordinal(date.getDate())
      formatted += ' ' + months[date.getMonth()]
      return formatted + ' ' + date.getFullYear()


      document.body.innerHTML = specialFormat(new Date())





      share|improve this answer
































        0














        As many has mentioned, here is another answer.



        This is directly based on @kennebec's answer, which I found the most simplest way to get this date Ordinal generated for given JavaScript date:



        I created two prototype function as follows:



        Date.prototype.getDateWithDateOrdinal = function() 
        var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
        if(d>3 && d<21) return d+'th';
        switch (d % 10)
        case 1: return d+"st";
        case 2: return d+"nd";
        case 3: return d+"rd";
        default: return d+"th";

        ;

        Date.prototype.getMonthName = function(shorten)
        var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
        var monthIndex = this.getMonth();
        var tempIndex = -1;
        if (monthIndex == 0) tempIndex = 0 ;
        if (monthIndex == 1) tempIndex = 1 ;
        if (monthIndex == 2) tempIndex = 2 ;
        if (monthIndex == 3) tempIndex = 3 ;
        if (monthIndex == 4) tempIndex = 4 ;
        if (monthIndex == 5) tempIndex = 5 ;
        if (monthIndex == 6) tempIndex = 6 ;
        if (monthIndex == 7) tempIndex = 7 ;
        if (monthIndex == 8) tempIndex = 8 ;
        if (monthIndex == 9) tempIndex = 9 ;
        if (monthIndex == 10) tempIndex = 10 ;
        if (monthIndex == 11) tempIndex = 11 ;

        if (tempIndex > -1)
        this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
        else
        this.monthName = "";


        return this.monthName;
        ;


        Note: just include the above prototype functions within your JS Script and use it as described bellow.



        And whenever there is a JS date I need to generate the date with date ordinal I use that prototype method as follows on that JS date:



        var myDate = new Date();
        // You may have to check your JS Console in the web browser to see the following
        console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

        // or I will update the Div. using jQuery
        $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());


        And it will print out date with date ordinal as shown in the following live demo:






        	Date.prototype.getMonthName = function(shorten) 
        var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
        var monthIndex = this.getMonth();
        var tempIndex = -1;
        if (monthIndex == 0) tempIndex = 0 ;
        if (monthIndex == 1) tempIndex = 1 ;
        if (monthIndex == 2) tempIndex = 2 ;
        if (monthIndex == 3) tempIndex = 3 ;
        if (monthIndex == 4) tempIndex = 4 ;
        if (monthIndex == 5) tempIndex = 5 ;
        if (monthIndex == 6) tempIndex = 6 ;
        if (monthIndex == 7) tempIndex = 7 ;
        if (monthIndex == 8) tempIndex = 8 ;
        if (monthIndex == 9) tempIndex = 9 ;
        if (monthIndex == 10) tempIndex = 10 ;
        if (monthIndex == 11) tempIndex = 11 ;

        if (tempIndex > -1)
        this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
        else
        this.monthName = "";


        return this.monthName;
        ;

        Date.prototype.getDateWithDateOrdinal = function()
        var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
        if(d>3 && d<21) return d+'th';
        switch (d % 10)
        case 1: return d+"st";
        case 2: return d+"nd";
        case 3: return d+"rd";
        default: return d+"th";

        ;

        var myDate = new Date();
        // You may have to check your JS Console in the web browser to see the following
        console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

        // or I will update the Div. using jQuery
        $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <p id="date"></p>





        .






        share|improve this answer
































          0














          A short and compact solution:






          function format(date, tmp)


          // 14 days from today

          console.log('14 days from today: ' +
          format(new Date(+new Date + 14 * 864e5)));

          // test formatting for all dates within a month from today

          var day = 864e5, today = +new Date;
          for(var i = 0; i < 32; i++)
          console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))





          (The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)






          share|improve this answer
































            0

















            	Date.prototype.getMonthName = function(shorten) 
            var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
            var monthIndex = this.getMonth();
            var tempIndex = -1;
            if (monthIndex == 0) tempIndex = 0 ;
            if (monthIndex == 1) tempIndex = 1 ;
            if (monthIndex == 2) tempIndex = 2 ;
            if (monthIndex == 3) tempIndex = 3 ;
            if (monthIndex == 4) tempIndex = 4 ;
            if (monthIndex == 5) tempIndex = 5 ;
            if (monthIndex == 6) tempIndex = 6 ;
            if (monthIndex == 7) tempIndex = 7 ;
            if (monthIndex == 8) tempIndex = 8 ;
            if (monthIndex == 9) tempIndex = 9 ;
            if (monthIndex == 10) tempIndex = 10 ;
            if (monthIndex == 11) tempIndex = 11 ;

            if (tempIndex > -1)
            this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
            else
            this.monthName = "";


            return this.monthName;
            ;

            Date.prototype.getDateWithDateOrdinal = function()
            var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
            if(d>3 && d<21) return d+'th';
            switch (d % 10)
            case 1: return d+"st";
            case 2: return d+"nd";
            case 3: return d+"rd";
            default: return d+"th";

            ;

            var myDate = new Date();
            // You may have to check your JS Console in the web browser to see the following
            console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

            // or I will update the Div. using jQuery
            $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <p id="date"></p>








            share|improve this answer






























              -1














              Super simple functional implementation:



              const ordinal = (d) => 
              const nth = '1': 'st', '2': 'nd', '3': 'rd'
              return `$d$nth[d] `


              const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']

              const dateString = (date) => `$ordinal(date.getDate()) $monthNames[date.getMonth()] $date.getFullYear()`

              // Use like this:
              dateString(new Date()) // 18th July 2016





              share|improve this answer























              • Unfortunately this method is too simple and doesn't work for days like the 21st.

                – Tom
                Oct 22 '16 at 23:36


















              -1














              Strongly inspired by @user2309185's.



              const ordinal = (d) => 'th')






              share|improve this answer


















              • 1





                I found an issue with this, 13 = 13rd

                – haakym
                Feb 16 '17 at 22:39


















              -1














              Here is an easy solution:



              var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd': 

              (today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));





              share|improve this answer

























              • running this in scratchpad gives "today not defined."

                – Nick
                Jan 7 '18 at 6:22










              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15397372%2fjavascript-new-date-ordinal-st-nd-rd-th%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              12 Answers
              12






              active

              oldest

              votes








              12 Answers
              12






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              56














              Here:



              JSFiddle






              var fortnightAway = new Date(+new Date + 12096e5),
              date = fortnightAway.getDate(),
              month = ["January","February","March","April","May","June","July",
              "August","September","October","November","December"][fortnightAway.getMonth()];

              function nth(d)
              if (d > 3 && d < 21) return 'th';
              switch (d % 10)
              case 1: return "st";
              case 2: return "nd";
              case 3: return "rd";
              default: return "th";


              document.body.innerHTML = date + nth(date) + " " +
              month + " " +
              fortnightAway.getFullYear();








              share|improve this answer




















              • 2





                Good work.... Thanks

                – Elby
                May 27 '14 at 6:03











              • Works perfect... +1 for d>3 && d<21

                – ĆĦΔŇĐŘΔ ŇΔҜҜΔ
                Apr 17 '15 at 10:25











              • For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";

                – krick
                Dec 15 '16 at 22:36












              • That is missing 21,22,23

                – mplungjan
                Dec 16 '16 at 5:12











              • if (d == 0) return ''; ... if used outside of a date context

                – John
                Apr 13 '18 at 14:47
















              56














              Here:



              JSFiddle






              var fortnightAway = new Date(+new Date + 12096e5),
              date = fortnightAway.getDate(),
              month = ["January","February","March","April","May","June","July",
              "August","September","October","November","December"][fortnightAway.getMonth()];

              function nth(d)
              if (d > 3 && d < 21) return 'th';
              switch (d % 10)
              case 1: return "st";
              case 2: return "nd";
              case 3: return "rd";
              default: return "th";


              document.body.innerHTML = date + nth(date) + " " +
              month + " " +
              fortnightAway.getFullYear();








              share|improve this answer




















              • 2





                Good work.... Thanks

                – Elby
                May 27 '14 at 6:03











              • Works perfect... +1 for d>3 && d<21

                – ĆĦΔŇĐŘΔ ŇΔҜҜΔ
                Apr 17 '15 at 10:25











              • For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";

                – krick
                Dec 15 '16 at 22:36












              • That is missing 21,22,23

                – mplungjan
                Dec 16 '16 at 5:12











              • if (d == 0) return ''; ... if used outside of a date context

                – John
                Apr 13 '18 at 14:47














              56












              56








              56







              Here:



              JSFiddle






              var fortnightAway = new Date(+new Date + 12096e5),
              date = fortnightAway.getDate(),
              month = ["January","February","March","April","May","June","July",
              "August","September","October","November","December"][fortnightAway.getMonth()];

              function nth(d)
              if (d > 3 && d < 21) return 'th';
              switch (d % 10)
              case 1: return "st";
              case 2: return "nd";
              case 3: return "rd";
              default: return "th";


              document.body.innerHTML = date + nth(date) + " " +
              month + " " +
              fortnightAway.getFullYear();








              share|improve this answer















              Here:



              JSFiddle






              var fortnightAway = new Date(+new Date + 12096e5),
              date = fortnightAway.getDate(),
              month = ["January","February","March","April","May","June","July",
              "August","September","October","November","December"][fortnightAway.getMonth()];

              function nth(d)
              if (d > 3 && d < 21) return 'th';
              switch (d % 10)
              case 1: return "st";
              case 2: return "nd";
              case 3: return "rd";
              default: return "th";


              document.body.innerHTML = date + nth(date) + " " +
              month + " " +
              fortnightAway.getFullYear();








              var fortnightAway = new Date(+new Date + 12096e5),
              date = fortnightAway.getDate(),
              month = ["January","February","March","April","May","June","July",
              "August","September","October","November","December"][fortnightAway.getMonth()];

              function nth(d)
              if (d > 3 && d < 21) return 'th';
              switch (d % 10)
              case 1: return "st";
              case 2: return "nd";
              case 3: return "rd";
              default: return "th";


              document.body.innerHTML = date + nth(date) + " " +
              month + " " +
              fortnightAway.getFullYear();





              var fortnightAway = new Date(+new Date + 12096e5),
              date = fortnightAway.getDate(),
              month = ["January","February","March","April","May","June","July",
              "August","September","October","November","December"][fortnightAway.getMonth()];

              function nth(d)
              if (d > 3 && d < 21) return 'th';
              switch (d % 10)
              case 1: return "st";
              case 2: return "nd";
              case 3: return "rd";
              default: return "th";


              document.body.innerHTML = date + nth(date) + " " +
              month + " " +
              fortnightAway.getFullYear();






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 12 '18 at 10:00

























              answered Mar 13 '13 at 22:11









              mplungjanmplungjan

              88.6k22126182




              88.6k22126182







              • 2





                Good work.... Thanks

                – Elby
                May 27 '14 at 6:03











              • Works perfect... +1 for d>3 && d<21

                – ĆĦΔŇĐŘΔ ŇΔҜҜΔ
                Apr 17 '15 at 10:25











              • For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";

                – krick
                Dec 15 '16 at 22:36












              • That is missing 21,22,23

                – mplungjan
                Dec 16 '16 at 5:12











              • if (d == 0) return ''; ... if used outside of a date context

                – John
                Apr 13 '18 at 14:47













              • 2





                Good work.... Thanks

                – Elby
                May 27 '14 at 6:03











              • Works perfect... +1 for d>3 && d<21

                – ĆĦΔŇĐŘΔ ŇΔҜҜΔ
                Apr 17 '15 at 10:25











              • For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";

                – krick
                Dec 15 '16 at 22:36












              • That is missing 21,22,23

                – mplungjan
                Dec 16 '16 at 5:12











              • if (d == 0) return ''; ... if used outside of a date context

                – John
                Apr 13 '18 at 14:47








              2




              2





              Good work.... Thanks

              – Elby
              May 27 '14 at 6:03





              Good work.... Thanks

              – Elby
              May 27 '14 at 6:03













              Works perfect... +1 for d>3 && d<21

              – ĆĦΔŇĐŘΔ ŇΔҜҜΔ
              Apr 17 '15 at 10:25





              Works perfect... +1 for d>3 && d<21

              – ĆĦΔŇĐŘΔ ŇΔҜҜΔ
              Apr 17 '15 at 10:25













              For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";

              – krick
              Dec 15 '16 at 22:36






              For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";

              – krick
              Dec 15 '16 at 22:36














              That is missing 21,22,23

              – mplungjan
              Dec 16 '16 at 5:12





              That is missing 21,22,23

              – mplungjan
              Dec 16 '16 at 5:12













              if (d == 0) return ''; ... if used outside of a date context

              – John
              Apr 13 '18 at 14:47






              if (d == 0) return ''; ... if used outside of a date context

              – John
              Apr 13 '18 at 14:47














              16














              Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.



              function getOrdinalNum(n) 
              return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21)





              share|improve this answer


















              • 1





                tested 1 to 31 and it works correctly

                – johnc
                Oct 22 '17 at 17:34















              16














              Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.



              function getOrdinalNum(n) 
              return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21)





              share|improve this answer


















              • 1





                tested 1 to 31 and it works correctly

                – johnc
                Oct 22 '17 at 17:34













              16












              16








              16







              Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.



              function getOrdinalNum(n) 
              return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21)





              share|improve this answer













              Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.



              function getOrdinalNum(n) 
              return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 7 '17 at 17:00









              The MartinThe Martin

              16112




              16112







              • 1





                tested 1 to 31 and it works correctly

                – johnc
                Oct 22 '17 at 17:34












              • 1





                tested 1 to 31 and it works correctly

                – johnc
                Oct 22 '17 at 17:34







              1




              1





              tested 1 to 31 and it works correctly

              – johnc
              Oct 22 '17 at 17:34





              tested 1 to 31 and it works correctly

              – johnc
              Oct 22 '17 at 17:34











              3














              Lots of formatting answers, so I'll just work on the nth of any integer-



              Number.prototype.nth= function()
              if(this%1) return this;
              var s= this%100;
              if(s>3 && s<21) return this+'th';
              switch(s%10)
              case 1: return this+'st';
              case 2: return this+'nd';
              case 3: return this+'rd';
              default: return this+'th';







              share|improve this answer

























              • The if is for teens, so 13 is 13th and not 13rd.

                – kennebec
                Mar 13 '13 at 22:32












              • Why the this%1? Mod 1 is always 0 so the statement will always fail, no?

                – Forage
                Mar 21 '18 at 11:10















              3














              Lots of formatting answers, so I'll just work on the nth of any integer-



              Number.prototype.nth= function()
              if(this%1) return this;
              var s= this%100;
              if(s>3 && s<21) return this+'th';
              switch(s%10)
              case 1: return this+'st';
              case 2: return this+'nd';
              case 3: return this+'rd';
              default: return this+'th';







              share|improve this answer

























              • The if is for teens, so 13 is 13th and not 13rd.

                – kennebec
                Mar 13 '13 at 22:32












              • Why the this%1? Mod 1 is always 0 so the statement will always fail, no?

                – Forage
                Mar 21 '18 at 11:10













              3












              3








              3







              Lots of formatting answers, so I'll just work on the nth of any integer-



              Number.prototype.nth= function()
              if(this%1) return this;
              var s= this%100;
              if(s>3 && s<21) return this+'th';
              switch(s%10)
              case 1: return this+'st';
              case 2: return this+'nd';
              case 3: return this+'rd';
              default: return this+'th';







              share|improve this answer















              Lots of formatting answers, so I'll just work on the nth of any integer-



              Number.prototype.nth= function()
              if(this%1) return this;
              var s= this%100;
              if(s>3 && s<21) return this+'th';
              switch(s%10)
              case 1: return this+'st';
              case 2: return this+'nd';
              case 3: return this+'rd';
              default: return this+'th';








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 13 '13 at 22:30

























              answered Mar 13 '13 at 22:14









              kennebeckennebec

              79.4k2186117




              79.4k2186117












              • The if is for teens, so 13 is 13th and not 13rd.

                – kennebec
                Mar 13 '13 at 22:32












              • Why the this%1? Mod 1 is always 0 so the statement will always fail, no?

                – Forage
                Mar 21 '18 at 11:10

















              • The if is for teens, so 13 is 13th and not 13rd.

                – kennebec
                Mar 13 '13 at 22:32












              • Why the this%1? Mod 1 is always 0 so the statement will always fail, no?

                – Forage
                Mar 21 '18 at 11:10
















              The if is for teens, so 13 is 13th and not 13rd.

              – kennebec
              Mar 13 '13 at 22:32






              The if is for teens, so 13 is 13th and not 13rd.

              – kennebec
              Mar 13 '13 at 22:32














              Why the this%1? Mod 1 is always 0 so the statement will always fail, no?

              – Forage
              Mar 21 '18 at 11:10





              Why the this%1? Mod 1 is always 0 so the statement will always fail, no?

              – Forage
              Mar 21 '18 at 11:10











              2














              I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.



              function dateOrdinal(dom) ;


              or compact version using conditional operators



              function dateOrdinal(d) 2==d?"nd":23==d;


              http://jsben.ch/#/DrBpl






              share|improve this answer





























                2














                I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.



                function dateOrdinal(dom) ;


                or compact version using conditional operators



                function dateOrdinal(d) 2==d?"nd":23==d;


                http://jsben.ch/#/DrBpl






                share|improve this answer



























                  2












                  2








                  2







                  I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.



                  function dateOrdinal(dom) ;


                  or compact version using conditional operators



                  function dateOrdinal(d) 2==d?"nd":23==d;


                  http://jsben.ch/#/DrBpl






                  share|improve this answer















                  I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.



                  function dateOrdinal(dom) ;


                  or compact version using conditional operators



                  function dateOrdinal(d) 2==d?"nd":23==d;


                  http://jsben.ch/#/DrBpl







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 15 '17 at 23:06

























                  answered Feb 15 '17 at 22:57









                  fredli74fredli74

                  213




                  213





















                      1














                      Lots of answers, here's another:



                      function addOrd(n) 

                      // Return date string two weeks from now (14 days) in
                      // format 13th March 2013
                      function formatDatePlusTwoWeeks(d)
                      var months = ['January','February','March','April','May','June',
                      'July','August','September','October','November','December'];

                      // Copy date object so don't modify original
                      var e = new Date(d);

                      // Add two weeks (14 days)
                      e.setDate(e.getDate() + 14);
                      return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();


                      alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013





                      share|improve this answer



























                        1














                        Lots of answers, here's another:



                        function addOrd(n) 

                        // Return date string two weeks from now (14 days) in
                        // format 13th March 2013
                        function formatDatePlusTwoWeeks(d)
                        var months = ['January','February','March','April','May','June',
                        'July','August','September','October','November','December'];

                        // Copy date object so don't modify original
                        var e = new Date(d);

                        // Add two weeks (14 days)
                        e.setDate(e.getDate() + 14);
                        return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();


                        alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013





                        share|improve this answer

























                          1












                          1








                          1







                          Lots of answers, here's another:



                          function addOrd(n) 

                          // Return date string two weeks from now (14 days) in
                          // format 13th March 2013
                          function formatDatePlusTwoWeeks(d)
                          var months = ['January','February','March','April','May','June',
                          'July','August','September','October','November','December'];

                          // Copy date object so don't modify original
                          var e = new Date(d);

                          // Add two weeks (14 days)
                          e.setDate(e.getDate() + 14);
                          return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();


                          alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013





                          share|improve this answer













                          Lots of answers, here's another:



                          function addOrd(n) 

                          // Return date string two weeks from now (14 days) in
                          // format 13th March 2013
                          function formatDatePlusTwoWeeks(d)
                          var months = ['January','February','March','April','May','June',
                          'July','August','September','October','November','December'];

                          // Copy date object so don't modify original
                          var e = new Date(d);

                          // Add two weeks (14 days)
                          e.setDate(e.getDate() + 14);
                          return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();


                          alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 13 '13 at 22:28









                          RobGRobG

                          98.4k19108146




                          98.4k19108146





















                              0














                              I'm a bit late to the party, but this should work:



                              function ordinal(number) 
                              number = Number(number)
                              if(!number

                              function specialFormat(date)
                              // add two weeks
                              date = new Date(+date + 12096e5)
                              var months = [
                              'January'
                              , 'February'
                              , 'March'
                              , 'April'
                              , 'May'
                              , 'June'
                              , 'July'
                              , 'August'
                              , 'September'
                              , 'October'
                              , 'November'
                              , 'December'
                              ]
                              var formatted = ordinal(date.getDate())
                              formatted += ' ' + months[date.getMonth()]
                              return formatted + ' ' + date.getFullYear()


                              document.body.innerHTML = specialFormat(new Date())





                              share|improve this answer





























                                0














                                I'm a bit late to the party, but this should work:



                                function ordinal(number) 
                                number = Number(number)
                                if(!number

                                function specialFormat(date)
                                // add two weeks
                                date = new Date(+date + 12096e5)
                                var months = [
                                'January'
                                , 'February'
                                , 'March'
                                , 'April'
                                , 'May'
                                , 'June'
                                , 'July'
                                , 'August'
                                , 'September'
                                , 'October'
                                , 'November'
                                , 'December'
                                ]
                                var formatted = ordinal(date.getDate())
                                formatted += ' ' + months[date.getMonth()]
                                return formatted + ' ' + date.getFullYear()


                                document.body.innerHTML = specialFormat(new Date())





                                share|improve this answer



























                                  0












                                  0








                                  0







                                  I'm a bit late to the party, but this should work:



                                  function ordinal(number) 
                                  number = Number(number)
                                  if(!number

                                  function specialFormat(date)
                                  // add two weeks
                                  date = new Date(+date + 12096e5)
                                  var months = [
                                  'January'
                                  , 'February'
                                  , 'March'
                                  , 'April'
                                  , 'May'
                                  , 'June'
                                  , 'July'
                                  , 'August'
                                  , 'September'
                                  , 'October'
                                  , 'November'
                                  , 'December'
                                  ]
                                  var formatted = ordinal(date.getDate())
                                  formatted += ' ' + months[date.getMonth()]
                                  return formatted + ' ' + date.getFullYear()


                                  document.body.innerHTML = specialFormat(new Date())





                                  share|improve this answer















                                  I'm a bit late to the party, but this should work:



                                  function ordinal(number) 
                                  number = Number(number)
                                  if(!number

                                  function specialFormat(date)
                                  // add two weeks
                                  date = new Date(+date + 12096e5)
                                  var months = [
                                  'January'
                                  , 'February'
                                  , 'March'
                                  , 'April'
                                  , 'May'
                                  , 'June'
                                  , 'July'
                                  , 'August'
                                  , 'September'
                                  , 'October'
                                  , 'November'
                                  , 'December'
                                  ]
                                  var formatted = ordinal(date.getDate())
                                  formatted += ' ' + months[date.getMonth()]
                                  return formatted + ' ' + date.getFullYear()


                                  document.body.innerHTML = specialFormat(new Date())






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Aug 8 '13 at 18:21

























                                  answered Aug 8 '13 at 18:15









                                  GabeGabe

                                  1365




                                  1365





















                                      0














                                      As many has mentioned, here is another answer.



                                      This is directly based on @kennebec's answer, which I found the most simplest way to get this date Ordinal generated for given JavaScript date:



                                      I created two prototype function as follows:



                                      Date.prototype.getDateWithDateOrdinal = function() 
                                      var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                      if(d>3 && d<21) return d+'th';
                                      switch (d % 10)
                                      case 1: return d+"st";
                                      case 2: return d+"nd";
                                      case 3: return d+"rd";
                                      default: return d+"th";

                                      ;

                                      Date.prototype.getMonthName = function(shorten)
                                      var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                      var monthIndex = this.getMonth();
                                      var tempIndex = -1;
                                      if (monthIndex == 0) tempIndex = 0 ;
                                      if (monthIndex == 1) tempIndex = 1 ;
                                      if (monthIndex == 2) tempIndex = 2 ;
                                      if (monthIndex == 3) tempIndex = 3 ;
                                      if (monthIndex == 4) tempIndex = 4 ;
                                      if (monthIndex == 5) tempIndex = 5 ;
                                      if (monthIndex == 6) tempIndex = 6 ;
                                      if (monthIndex == 7) tempIndex = 7 ;
                                      if (monthIndex == 8) tempIndex = 8 ;
                                      if (monthIndex == 9) tempIndex = 9 ;
                                      if (monthIndex == 10) tempIndex = 10 ;
                                      if (monthIndex == 11) tempIndex = 11 ;

                                      if (tempIndex > -1)
                                      this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                      else
                                      this.monthName = "";


                                      return this.monthName;
                                      ;


                                      Note: just include the above prototype functions within your JS Script and use it as described bellow.



                                      And whenever there is a JS date I need to generate the date with date ordinal I use that prototype method as follows on that JS date:



                                      var myDate = new Date();
                                      // You may have to check your JS Console in the web browser to see the following
                                      console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                      // or I will update the Div. using jQuery
                                      $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());


                                      And it will print out date with date ordinal as shown in the following live demo:






                                      	Date.prototype.getMonthName = function(shorten) 
                                      var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                      var monthIndex = this.getMonth();
                                      var tempIndex = -1;
                                      if (monthIndex == 0) tempIndex = 0 ;
                                      if (monthIndex == 1) tempIndex = 1 ;
                                      if (monthIndex == 2) tempIndex = 2 ;
                                      if (monthIndex == 3) tempIndex = 3 ;
                                      if (monthIndex == 4) tempIndex = 4 ;
                                      if (monthIndex == 5) tempIndex = 5 ;
                                      if (monthIndex == 6) tempIndex = 6 ;
                                      if (monthIndex == 7) tempIndex = 7 ;
                                      if (monthIndex == 8) tempIndex = 8 ;
                                      if (monthIndex == 9) tempIndex = 9 ;
                                      if (monthIndex == 10) tempIndex = 10 ;
                                      if (monthIndex == 11) tempIndex = 11 ;

                                      if (tempIndex > -1)
                                      this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                      else
                                      this.monthName = "";


                                      return this.monthName;
                                      ;

                                      Date.prototype.getDateWithDateOrdinal = function()
                                      var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                      if(d>3 && d<21) return d+'th';
                                      switch (d % 10)
                                      case 1: return d+"st";
                                      case 2: return d+"nd";
                                      case 3: return d+"rd";
                                      default: return d+"th";

                                      ;

                                      var myDate = new Date();
                                      // You may have to check your JS Console in the web browser to see the following
                                      console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                      // or I will update the Div. using jQuery
                                      $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                      <p id="date"></p>





                                      .






                                      share|improve this answer





























                                        0














                                        As many has mentioned, here is another answer.



                                        This is directly based on @kennebec's answer, which I found the most simplest way to get this date Ordinal generated for given JavaScript date:



                                        I created two prototype function as follows:



                                        Date.prototype.getDateWithDateOrdinal = function() 
                                        var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                        if(d>3 && d<21) return d+'th';
                                        switch (d % 10)
                                        case 1: return d+"st";
                                        case 2: return d+"nd";
                                        case 3: return d+"rd";
                                        default: return d+"th";

                                        ;

                                        Date.prototype.getMonthName = function(shorten)
                                        var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                        var monthIndex = this.getMonth();
                                        var tempIndex = -1;
                                        if (monthIndex == 0) tempIndex = 0 ;
                                        if (monthIndex == 1) tempIndex = 1 ;
                                        if (monthIndex == 2) tempIndex = 2 ;
                                        if (monthIndex == 3) tempIndex = 3 ;
                                        if (monthIndex == 4) tempIndex = 4 ;
                                        if (monthIndex == 5) tempIndex = 5 ;
                                        if (monthIndex == 6) tempIndex = 6 ;
                                        if (monthIndex == 7) tempIndex = 7 ;
                                        if (monthIndex == 8) tempIndex = 8 ;
                                        if (monthIndex == 9) tempIndex = 9 ;
                                        if (monthIndex == 10) tempIndex = 10 ;
                                        if (monthIndex == 11) tempIndex = 11 ;

                                        if (tempIndex > -1)
                                        this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                        else
                                        this.monthName = "";


                                        return this.monthName;
                                        ;


                                        Note: just include the above prototype functions within your JS Script and use it as described bellow.



                                        And whenever there is a JS date I need to generate the date with date ordinal I use that prototype method as follows on that JS date:



                                        var myDate = new Date();
                                        // You may have to check your JS Console in the web browser to see the following
                                        console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                        // or I will update the Div. using jQuery
                                        $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());


                                        And it will print out date with date ordinal as shown in the following live demo:






                                        	Date.prototype.getMonthName = function(shorten) 
                                        var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                        var monthIndex = this.getMonth();
                                        var tempIndex = -1;
                                        if (monthIndex == 0) tempIndex = 0 ;
                                        if (monthIndex == 1) tempIndex = 1 ;
                                        if (monthIndex == 2) tempIndex = 2 ;
                                        if (monthIndex == 3) tempIndex = 3 ;
                                        if (monthIndex == 4) tempIndex = 4 ;
                                        if (monthIndex == 5) tempIndex = 5 ;
                                        if (monthIndex == 6) tempIndex = 6 ;
                                        if (monthIndex == 7) tempIndex = 7 ;
                                        if (monthIndex == 8) tempIndex = 8 ;
                                        if (monthIndex == 9) tempIndex = 9 ;
                                        if (monthIndex == 10) tempIndex = 10 ;
                                        if (monthIndex == 11) tempIndex = 11 ;

                                        if (tempIndex > -1)
                                        this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                        else
                                        this.monthName = "";


                                        return this.monthName;
                                        ;

                                        Date.prototype.getDateWithDateOrdinal = function()
                                        var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                        if(d>3 && d<21) return d+'th';
                                        switch (d % 10)
                                        case 1: return d+"st";
                                        case 2: return d+"nd";
                                        case 3: return d+"rd";
                                        default: return d+"th";

                                        ;

                                        var myDate = new Date();
                                        // You may have to check your JS Console in the web browser to see the following
                                        console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                        // or I will update the Div. using jQuery
                                        $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                        <p id="date"></p>





                                        .






                                        share|improve this answer



























                                          0












                                          0








                                          0







                                          As many has mentioned, here is another answer.



                                          This is directly based on @kennebec's answer, which I found the most simplest way to get this date Ordinal generated for given JavaScript date:



                                          I created two prototype function as follows:



                                          Date.prototype.getDateWithDateOrdinal = function() 
                                          var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                          if(d>3 && d<21) return d+'th';
                                          switch (d % 10)
                                          case 1: return d+"st";
                                          case 2: return d+"nd";
                                          case 3: return d+"rd";
                                          default: return d+"th";

                                          ;

                                          Date.prototype.getMonthName = function(shorten)
                                          var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                          var monthIndex = this.getMonth();
                                          var tempIndex = -1;
                                          if (monthIndex == 0) tempIndex = 0 ;
                                          if (monthIndex == 1) tempIndex = 1 ;
                                          if (monthIndex == 2) tempIndex = 2 ;
                                          if (monthIndex == 3) tempIndex = 3 ;
                                          if (monthIndex == 4) tempIndex = 4 ;
                                          if (monthIndex == 5) tempIndex = 5 ;
                                          if (monthIndex == 6) tempIndex = 6 ;
                                          if (monthIndex == 7) tempIndex = 7 ;
                                          if (monthIndex == 8) tempIndex = 8 ;
                                          if (monthIndex == 9) tempIndex = 9 ;
                                          if (monthIndex == 10) tempIndex = 10 ;
                                          if (monthIndex == 11) tempIndex = 11 ;

                                          if (tempIndex > -1)
                                          this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                          else
                                          this.monthName = "";


                                          return this.monthName;
                                          ;


                                          Note: just include the above prototype functions within your JS Script and use it as described bellow.



                                          And whenever there is a JS date I need to generate the date with date ordinal I use that prototype method as follows on that JS date:



                                          var myDate = new Date();
                                          // You may have to check your JS Console in the web browser to see the following
                                          console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                          // or I will update the Div. using jQuery
                                          $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());


                                          And it will print out date with date ordinal as shown in the following live demo:






                                          	Date.prototype.getMonthName = function(shorten) 
                                          var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                          var monthIndex = this.getMonth();
                                          var tempIndex = -1;
                                          if (monthIndex == 0) tempIndex = 0 ;
                                          if (monthIndex == 1) tempIndex = 1 ;
                                          if (monthIndex == 2) tempIndex = 2 ;
                                          if (monthIndex == 3) tempIndex = 3 ;
                                          if (monthIndex == 4) tempIndex = 4 ;
                                          if (monthIndex == 5) tempIndex = 5 ;
                                          if (monthIndex == 6) tempIndex = 6 ;
                                          if (monthIndex == 7) tempIndex = 7 ;
                                          if (monthIndex == 8) tempIndex = 8 ;
                                          if (monthIndex == 9) tempIndex = 9 ;
                                          if (monthIndex == 10) tempIndex = 10 ;
                                          if (monthIndex == 11) tempIndex = 11 ;

                                          if (tempIndex > -1)
                                          this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                          else
                                          this.monthName = "";


                                          return this.monthName;
                                          ;

                                          Date.prototype.getDateWithDateOrdinal = function()
                                          var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                          if(d>3 && d<21) return d+'th';
                                          switch (d % 10)
                                          case 1: return d+"st";
                                          case 2: return d+"nd";
                                          case 3: return d+"rd";
                                          default: return d+"th";

                                          ;

                                          var myDate = new Date();
                                          // You may have to check your JS Console in the web browser to see the following
                                          console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                          // or I will update the Div. using jQuery
                                          $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                          <p id="date"></p>





                                          .






                                          share|improve this answer















                                          As many has mentioned, here is another answer.



                                          This is directly based on @kennebec's answer, which I found the most simplest way to get this date Ordinal generated for given JavaScript date:



                                          I created two prototype function as follows:



                                          Date.prototype.getDateWithDateOrdinal = function() 
                                          var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                          if(d>3 && d<21) return d+'th';
                                          switch (d % 10)
                                          case 1: return d+"st";
                                          case 2: return d+"nd";
                                          case 3: return d+"rd";
                                          default: return d+"th";

                                          ;

                                          Date.prototype.getMonthName = function(shorten)
                                          var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                          var monthIndex = this.getMonth();
                                          var tempIndex = -1;
                                          if (monthIndex == 0) tempIndex = 0 ;
                                          if (monthIndex == 1) tempIndex = 1 ;
                                          if (monthIndex == 2) tempIndex = 2 ;
                                          if (monthIndex == 3) tempIndex = 3 ;
                                          if (monthIndex == 4) tempIndex = 4 ;
                                          if (monthIndex == 5) tempIndex = 5 ;
                                          if (monthIndex == 6) tempIndex = 6 ;
                                          if (monthIndex == 7) tempIndex = 7 ;
                                          if (monthIndex == 8) tempIndex = 8 ;
                                          if (monthIndex == 9) tempIndex = 9 ;
                                          if (monthIndex == 10) tempIndex = 10 ;
                                          if (monthIndex == 11) tempIndex = 11 ;

                                          if (tempIndex > -1)
                                          this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                          else
                                          this.monthName = "";


                                          return this.monthName;
                                          ;


                                          Note: just include the above prototype functions within your JS Script and use it as described bellow.



                                          And whenever there is a JS date I need to generate the date with date ordinal I use that prototype method as follows on that JS date:



                                          var myDate = new Date();
                                          // You may have to check your JS Console in the web browser to see the following
                                          console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                          // or I will update the Div. using jQuery
                                          $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());


                                          And it will print out date with date ordinal as shown in the following live demo:






                                          	Date.prototype.getMonthName = function(shorten) 
                                          var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                          var monthIndex = this.getMonth();
                                          var tempIndex = -1;
                                          if (monthIndex == 0) tempIndex = 0 ;
                                          if (monthIndex == 1) tempIndex = 1 ;
                                          if (monthIndex == 2) tempIndex = 2 ;
                                          if (monthIndex == 3) tempIndex = 3 ;
                                          if (monthIndex == 4) tempIndex = 4 ;
                                          if (monthIndex == 5) tempIndex = 5 ;
                                          if (monthIndex == 6) tempIndex = 6 ;
                                          if (monthIndex == 7) tempIndex = 7 ;
                                          if (monthIndex == 8) tempIndex = 8 ;
                                          if (monthIndex == 9) tempIndex = 9 ;
                                          if (monthIndex == 10) tempIndex = 10 ;
                                          if (monthIndex == 11) tempIndex = 11 ;

                                          if (tempIndex > -1)
                                          this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                          else
                                          this.monthName = "";


                                          return this.monthName;
                                          ;

                                          Date.prototype.getDateWithDateOrdinal = function()
                                          var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                          if(d>3 && d<21) return d+'th';
                                          switch (d % 10)
                                          case 1: return d+"st";
                                          case 2: return d+"nd";
                                          case 3: return d+"rd";
                                          default: return d+"th";

                                          ;

                                          var myDate = new Date();
                                          // You may have to check your JS Console in the web browser to see the following
                                          console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                          // or I will update the Div. using jQuery
                                          $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                          <p id="date"></p>





                                          .






                                          	Date.prototype.getMonthName = function(shorten) 
                                          var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                          var monthIndex = this.getMonth();
                                          var tempIndex = -1;
                                          if (monthIndex == 0) tempIndex = 0 ;
                                          if (monthIndex == 1) tempIndex = 1 ;
                                          if (monthIndex == 2) tempIndex = 2 ;
                                          if (monthIndex == 3) tempIndex = 3 ;
                                          if (monthIndex == 4) tempIndex = 4 ;
                                          if (monthIndex == 5) tempIndex = 5 ;
                                          if (monthIndex == 6) tempIndex = 6 ;
                                          if (monthIndex == 7) tempIndex = 7 ;
                                          if (monthIndex == 8) tempIndex = 8 ;
                                          if (monthIndex == 9) tempIndex = 9 ;
                                          if (monthIndex == 10) tempIndex = 10 ;
                                          if (monthIndex == 11) tempIndex = 11 ;

                                          if (tempIndex > -1)
                                          this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                          else
                                          this.monthName = "";


                                          return this.monthName;
                                          ;

                                          Date.prototype.getDateWithDateOrdinal = function()
                                          var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                          if(d>3 && d<21) return d+'th';
                                          switch (d % 10)
                                          case 1: return d+"st";
                                          case 2: return d+"nd";
                                          case 3: return d+"rd";
                                          default: return d+"th";

                                          ;

                                          var myDate = new Date();
                                          // You may have to check your JS Console in the web browser to see the following
                                          console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                          // or I will update the Div. using jQuery
                                          $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                          <p id="date"></p>





                                          	Date.prototype.getMonthName = function(shorten) 
                                          var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                          var monthIndex = this.getMonth();
                                          var tempIndex = -1;
                                          if (monthIndex == 0) tempIndex = 0 ;
                                          if (monthIndex == 1) tempIndex = 1 ;
                                          if (monthIndex == 2) tempIndex = 2 ;
                                          if (monthIndex == 3) tempIndex = 3 ;
                                          if (monthIndex == 4) tempIndex = 4 ;
                                          if (monthIndex == 5) tempIndex = 5 ;
                                          if (monthIndex == 6) tempIndex = 6 ;
                                          if (monthIndex == 7) tempIndex = 7 ;
                                          if (monthIndex == 8) tempIndex = 8 ;
                                          if (monthIndex == 9) tempIndex = 9 ;
                                          if (monthIndex == 10) tempIndex = 10 ;
                                          if (monthIndex == 11) tempIndex = 11 ;

                                          if (tempIndex > -1)
                                          this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                          else
                                          this.monthName = "";


                                          return this.monthName;
                                          ;

                                          Date.prototype.getDateWithDateOrdinal = function()
                                          var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                          if(d>3 && d<21) return d+'th';
                                          switch (d % 10)
                                          case 1: return d+"st";
                                          case 2: return d+"nd";
                                          case 3: return d+"rd";
                                          default: return d+"th";

                                          ;

                                          var myDate = new Date();
                                          // You may have to check your JS Console in the web browser to see the following
                                          console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                          // or I will update the Div. using jQuery
                                          $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                          <p id="date"></p>






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Oct 14 '15 at 20:05

























                                          answered Oct 14 '15 at 19:49









                                          Randika VishmanRandika Vishman

                                          4,72723958




                                          4,72723958





















                                              0














                                              A short and compact solution:






                                              function format(date, tmp)


                                              // 14 days from today

                                              console.log('14 days from today: ' +
                                              format(new Date(+new Date + 14 * 864e5)));

                                              // test formatting for all dates within a month from today

                                              var day = 864e5, today = +new Date;
                                              for(var i = 0; i < 32; i++)
                                              console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))





                                              (The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)






                                              share|improve this answer





























                                                0














                                                A short and compact solution:






                                                function format(date, tmp)


                                                // 14 days from today

                                                console.log('14 days from today: ' +
                                                format(new Date(+new Date + 14 * 864e5)));

                                                // test formatting for all dates within a month from today

                                                var day = 864e5, today = +new Date;
                                                for(var i = 0; i < 32; i++)
                                                console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))





                                                (The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)






                                                share|improve this answer



























                                                  0












                                                  0








                                                  0







                                                  A short and compact solution:






                                                  function format(date, tmp)


                                                  // 14 days from today

                                                  console.log('14 days from today: ' +
                                                  format(new Date(+new Date + 14 * 864e5)));

                                                  // test formatting for all dates within a month from today

                                                  var day = 864e5, today = +new Date;
                                                  for(var i = 0; i < 32; i++)
                                                  console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))





                                                  (The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)






                                                  share|improve this answer















                                                  A short and compact solution:






                                                  function format(date, tmp)


                                                  // 14 days from today

                                                  console.log('14 days from today: ' +
                                                  format(new Date(+new Date + 14 * 864e5)));

                                                  // test formatting for all dates within a month from today

                                                  var day = 864e5, today = +new Date;
                                                  for(var i = 0; i < 32; i++)
                                                  console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))





                                                  (The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)






                                                  function format(date, tmp)


                                                  // 14 days from today

                                                  console.log('14 days from today: ' +
                                                  format(new Date(+new Date + 14 * 864e5)));

                                                  // test formatting for all dates within a month from today

                                                  var day = 864e5, today = +new Date;
                                                  for(var i = 0; i < 32; i++)
                                                  console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))





                                                  function format(date, tmp)


                                                  // 14 days from today

                                                  console.log('14 days from today: ' +
                                                  format(new Date(+new Date + 14 * 864e5)));

                                                  // test formatting for all dates within a month from today

                                                  var day = 864e5, today = +new Date;
                                                  for(var i = 0; i < 32; i++)
                                                  console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited May 16 '17 at 17:16

























                                                  answered May 14 '17 at 19:33









                                                  Tomas LangkaasTomas Langkaas

                                                  2,5661725




                                                  2,5661725





















                                                      0

















                                                      	Date.prototype.getMonthName = function(shorten) 
                                                      var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                                      var monthIndex = this.getMonth();
                                                      var tempIndex = -1;
                                                      if (monthIndex == 0) tempIndex = 0 ;
                                                      if (monthIndex == 1) tempIndex = 1 ;
                                                      if (monthIndex == 2) tempIndex = 2 ;
                                                      if (monthIndex == 3) tempIndex = 3 ;
                                                      if (monthIndex == 4) tempIndex = 4 ;
                                                      if (monthIndex == 5) tempIndex = 5 ;
                                                      if (monthIndex == 6) tempIndex = 6 ;
                                                      if (monthIndex == 7) tempIndex = 7 ;
                                                      if (monthIndex == 8) tempIndex = 8 ;
                                                      if (monthIndex == 9) tempIndex = 9 ;
                                                      if (monthIndex == 10) tempIndex = 10 ;
                                                      if (monthIndex == 11) tempIndex = 11 ;

                                                      if (tempIndex > -1)
                                                      this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                                      else
                                                      this.monthName = "";


                                                      return this.monthName;
                                                      ;

                                                      Date.prototype.getDateWithDateOrdinal = function()
                                                      var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                                      if(d>3 && d<21) return d+'th';
                                                      switch (d % 10)
                                                      case 1: return d+"st";
                                                      case 2: return d+"nd";
                                                      case 3: return d+"rd";
                                                      default: return d+"th";

                                                      ;

                                                      var myDate = new Date();
                                                      // You may have to check your JS Console in the web browser to see the following
                                                      console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                      // or I will update the Div. using jQuery
                                                      $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                                      <p id="date"></p>








                                                      share|improve this answer



























                                                        0

















                                                        	Date.prototype.getMonthName = function(shorten) 
                                                        var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                                        var monthIndex = this.getMonth();
                                                        var tempIndex = -1;
                                                        if (monthIndex == 0) tempIndex = 0 ;
                                                        if (monthIndex == 1) tempIndex = 1 ;
                                                        if (monthIndex == 2) tempIndex = 2 ;
                                                        if (monthIndex == 3) tempIndex = 3 ;
                                                        if (monthIndex == 4) tempIndex = 4 ;
                                                        if (monthIndex == 5) tempIndex = 5 ;
                                                        if (monthIndex == 6) tempIndex = 6 ;
                                                        if (monthIndex == 7) tempIndex = 7 ;
                                                        if (monthIndex == 8) tempIndex = 8 ;
                                                        if (monthIndex == 9) tempIndex = 9 ;
                                                        if (monthIndex == 10) tempIndex = 10 ;
                                                        if (monthIndex == 11) tempIndex = 11 ;

                                                        if (tempIndex > -1)
                                                        this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                                        else
                                                        this.monthName = "";


                                                        return this.monthName;
                                                        ;

                                                        Date.prototype.getDateWithDateOrdinal = function()
                                                        var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                                        if(d>3 && d<21) return d+'th';
                                                        switch (d % 10)
                                                        case 1: return d+"st";
                                                        case 2: return d+"nd";
                                                        case 3: return d+"rd";
                                                        default: return d+"th";

                                                        ;

                                                        var myDate = new Date();
                                                        // You may have to check your JS Console in the web browser to see the following
                                                        console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                        // or I will update the Div. using jQuery
                                                        $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                                        <p id="date"></p>








                                                        share|improve this answer

























                                                          0












                                                          0








                                                          0










                                                          	Date.prototype.getMonthName = function(shorten) 
                                                          var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                                          var monthIndex = this.getMonth();
                                                          var tempIndex = -1;
                                                          if (monthIndex == 0) tempIndex = 0 ;
                                                          if (monthIndex == 1) tempIndex = 1 ;
                                                          if (monthIndex == 2) tempIndex = 2 ;
                                                          if (monthIndex == 3) tempIndex = 3 ;
                                                          if (monthIndex == 4) tempIndex = 4 ;
                                                          if (monthIndex == 5) tempIndex = 5 ;
                                                          if (monthIndex == 6) tempIndex = 6 ;
                                                          if (monthIndex == 7) tempIndex = 7 ;
                                                          if (monthIndex == 8) tempIndex = 8 ;
                                                          if (monthIndex == 9) tempIndex = 9 ;
                                                          if (monthIndex == 10) tempIndex = 10 ;
                                                          if (monthIndex == 11) tempIndex = 11 ;

                                                          if (tempIndex > -1)
                                                          this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                                          else
                                                          this.monthName = "";


                                                          return this.monthName;
                                                          ;

                                                          Date.prototype.getDateWithDateOrdinal = function()
                                                          var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                                          if(d>3 && d<21) return d+'th';
                                                          switch (d % 10)
                                                          case 1: return d+"st";
                                                          case 2: return d+"nd";
                                                          case 3: return d+"rd";
                                                          default: return d+"th";

                                                          ;

                                                          var myDate = new Date();
                                                          // You may have to check your JS Console in the web browser to see the following
                                                          console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                          // or I will update the Div. using jQuery
                                                          $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                                          <p id="date"></p>








                                                          share|improve this answer
















                                                          	Date.prototype.getMonthName = function(shorten) 
                                                          var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                                          var monthIndex = this.getMonth();
                                                          var tempIndex = -1;
                                                          if (monthIndex == 0) tempIndex = 0 ;
                                                          if (monthIndex == 1) tempIndex = 1 ;
                                                          if (monthIndex == 2) tempIndex = 2 ;
                                                          if (monthIndex == 3) tempIndex = 3 ;
                                                          if (monthIndex == 4) tempIndex = 4 ;
                                                          if (monthIndex == 5) tempIndex = 5 ;
                                                          if (monthIndex == 6) tempIndex = 6 ;
                                                          if (monthIndex == 7) tempIndex = 7 ;
                                                          if (monthIndex == 8) tempIndex = 8 ;
                                                          if (monthIndex == 9) tempIndex = 9 ;
                                                          if (monthIndex == 10) tempIndex = 10 ;
                                                          if (monthIndex == 11) tempIndex = 11 ;

                                                          if (tempIndex > -1)
                                                          this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                                          else
                                                          this.monthName = "";


                                                          return this.monthName;
                                                          ;

                                                          Date.prototype.getDateWithDateOrdinal = function()
                                                          var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                                          if(d>3 && d<21) return d+'th';
                                                          switch (d % 10)
                                                          case 1: return d+"st";
                                                          case 2: return d+"nd";
                                                          case 3: return d+"rd";
                                                          default: return d+"th";

                                                          ;

                                                          var myDate = new Date();
                                                          // You may have to check your JS Console in the web browser to see the following
                                                          console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                          // or I will update the Div. using jQuery
                                                          $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                                          <p id="date"></p>








                                                          	Date.prototype.getMonthName = function(shorten) 
                                                          var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                                          var monthIndex = this.getMonth();
                                                          var tempIndex = -1;
                                                          if (monthIndex == 0) tempIndex = 0 ;
                                                          if (monthIndex == 1) tempIndex = 1 ;
                                                          if (monthIndex == 2) tempIndex = 2 ;
                                                          if (monthIndex == 3) tempIndex = 3 ;
                                                          if (monthIndex == 4) tempIndex = 4 ;
                                                          if (monthIndex == 5) tempIndex = 5 ;
                                                          if (monthIndex == 6) tempIndex = 6 ;
                                                          if (monthIndex == 7) tempIndex = 7 ;
                                                          if (monthIndex == 8) tempIndex = 8 ;
                                                          if (monthIndex == 9) tempIndex = 9 ;
                                                          if (monthIndex == 10) tempIndex = 10 ;
                                                          if (monthIndex == 11) tempIndex = 11 ;

                                                          if (tempIndex > -1)
                                                          this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                                          else
                                                          this.monthName = "";


                                                          return this.monthName;
                                                          ;

                                                          Date.prototype.getDateWithDateOrdinal = function()
                                                          var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                                          if(d>3 && d<21) return d+'th';
                                                          switch (d % 10)
                                                          case 1: return d+"st";
                                                          case 2: return d+"nd";
                                                          case 3: return d+"rd";
                                                          default: return d+"th";

                                                          ;

                                                          var myDate = new Date();
                                                          // You may have to check your JS Console in the web browser to see the following
                                                          console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                          // or I will update the Div. using jQuery
                                                          $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                                          <p id="date"></p>





                                                          	Date.prototype.getMonthName = function(shorten) 
                                                          var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                                                          var monthIndex = this.getMonth();
                                                          var tempIndex = -1;
                                                          if (monthIndex == 0) tempIndex = 0 ;
                                                          if (monthIndex == 1) tempIndex = 1 ;
                                                          if (monthIndex == 2) tempIndex = 2 ;
                                                          if (monthIndex == 3) tempIndex = 3 ;
                                                          if (monthIndex == 4) tempIndex = 4 ;
                                                          if (monthIndex == 5) tempIndex = 5 ;
                                                          if (monthIndex == 6) tempIndex = 6 ;
                                                          if (monthIndex == 7) tempIndex = 7 ;
                                                          if (monthIndex == 8) tempIndex = 8 ;
                                                          if (monthIndex == 9) tempIndex = 9 ;
                                                          if (monthIndex == 10) tempIndex = 10 ;
                                                          if (monthIndex == 11) tempIndex = 11 ;

                                                          if (tempIndex > -1)
                                                          this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
                                                          else
                                                          this.monthName = "";


                                                          return this.monthName;
                                                          ;

                                                          Date.prototype.getDateWithDateOrdinal = function()
                                                          var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
                                                          if(d>3 && d<21) return d+'th';
                                                          switch (d % 10)
                                                          case 1: return d+"st";
                                                          case 2: return d+"nd";
                                                          case 3: return d+"rd";
                                                          default: return d+"th";

                                                          ;

                                                          var myDate = new Date();
                                                          // You may have to check your JS Console in the web browser to see the following
                                                          console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                          // or I will update the Div. using jQuery
                                                          $('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());

                                                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                                                          <p id="date"></p>






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Apr 14 '18 at 19:12









                                                          user3516819user3516819

                                                          12




                                                          12





















                                                              -1














                                                              Super simple functional implementation:



                                                              const ordinal = (d) => 
                                                              const nth = '1': 'st', '2': 'nd', '3': 'rd'
                                                              return `$d$nth[d] `


                                                              const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']

                                                              const dateString = (date) => `$ordinal(date.getDate()) $monthNames[date.getMonth()] $date.getFullYear()`

                                                              // Use like this:
                                                              dateString(new Date()) // 18th July 2016





                                                              share|improve this answer























                                                              • Unfortunately this method is too simple and doesn't work for days like the 21st.

                                                                – Tom
                                                                Oct 22 '16 at 23:36















                                                              -1














                                                              Super simple functional implementation:



                                                              const ordinal = (d) => 
                                                              const nth = '1': 'st', '2': 'nd', '3': 'rd'
                                                              return `$d$nth[d] `


                                                              const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']

                                                              const dateString = (date) => `$ordinal(date.getDate()) $monthNames[date.getMonth()] $date.getFullYear()`

                                                              // Use like this:
                                                              dateString(new Date()) // 18th July 2016





                                                              share|improve this answer























                                                              • Unfortunately this method is too simple and doesn't work for days like the 21st.

                                                                – Tom
                                                                Oct 22 '16 at 23:36













                                                              -1












                                                              -1








                                                              -1







                                                              Super simple functional implementation:



                                                              const ordinal = (d) => 
                                                              const nth = '1': 'st', '2': 'nd', '3': 'rd'
                                                              return `$d$nth[d] `


                                                              const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']

                                                              const dateString = (date) => `$ordinal(date.getDate()) $monthNames[date.getMonth()] $date.getFullYear()`

                                                              // Use like this:
                                                              dateString(new Date()) // 18th July 2016





                                                              share|improve this answer













                                                              Super simple functional implementation:



                                                              const ordinal = (d) => 
                                                              const nth = '1': 'st', '2': 'nd', '3': 'rd'
                                                              return `$d$nth[d] `


                                                              const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']

                                                              const dateString = (date) => `$ordinal(date.getDate()) $monthNames[date.getMonth()] $date.getFullYear()`

                                                              // Use like this:
                                                              dateString(new Date()) // 18th July 2016






                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Jul 19 '16 at 3:50









                                                              user2309185user2309185

                                                              1731210




                                                              1731210












                                                              • Unfortunately this method is too simple and doesn't work for days like the 21st.

                                                                – Tom
                                                                Oct 22 '16 at 23:36

















                                                              • Unfortunately this method is too simple and doesn't work for days like the 21st.

                                                                – Tom
                                                                Oct 22 '16 at 23:36
















                                                              Unfortunately this method is too simple and doesn't work for days like the 21st.

                                                              – Tom
                                                              Oct 22 '16 at 23:36





                                                              Unfortunately this method is too simple and doesn't work for days like the 21st.

                                                              – Tom
                                                              Oct 22 '16 at 23:36











                                                              -1














                                                              Strongly inspired by @user2309185's.



                                                              const ordinal = (d) => 'th')






                                                              share|improve this answer


















                                                              • 1





                                                                I found an issue with this, 13 = 13rd

                                                                – haakym
                                                                Feb 16 '17 at 22:39















                                                              -1














                                                              Strongly inspired by @user2309185's.



                                                              const ordinal = (d) => 'th')






                                                              share|improve this answer


















                                                              • 1





                                                                I found an issue with this, 13 = 13rd

                                                                – haakym
                                                                Feb 16 '17 at 22:39













                                                              -1












                                                              -1








                                                              -1







                                                              Strongly inspired by @user2309185's.



                                                              const ordinal = (d) => 'th')






                                                              share|improve this answer













                                                              Strongly inspired by @user2309185's.



                                                              const ordinal = (d) => 'th')







                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Jan 26 '17 at 11:26









                                                              Adán Sánchez de Pedro CrespoAdán Sánchez de Pedro Crespo

                                                              273




                                                              273







                                                              • 1





                                                                I found an issue with this, 13 = 13rd

                                                                – haakym
                                                                Feb 16 '17 at 22:39












                                                              • 1





                                                                I found an issue with this, 13 = 13rd

                                                                – haakym
                                                                Feb 16 '17 at 22:39







                                                              1




                                                              1





                                                              I found an issue with this, 13 = 13rd

                                                              – haakym
                                                              Feb 16 '17 at 22:39





                                                              I found an issue with this, 13 = 13rd

                                                              – haakym
                                                              Feb 16 '17 at 22:39











                                                              -1














                                                              Here is an easy solution:



                                                              var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd': 

                                                              (today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));





                                                              share|improve this answer

























                                                              • running this in scratchpad gives "today not defined."

                                                                – Nick
                                                                Jan 7 '18 at 6:22















                                                              -1














                                                              Here is an easy solution:



                                                              var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd': 

                                                              (today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));





                                                              share|improve this answer

























                                                              • running this in scratchpad gives "today not defined."

                                                                – Nick
                                                                Jan 7 '18 at 6:22













                                                              -1












                                                              -1








                                                              -1







                                                              Here is an easy solution:



                                                              var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd': 

                                                              (today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));





                                                              share|improve this answer















                                                              Here is an easy solution:



                                                              var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd': 

                                                              (today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));






                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Jan 7 '18 at 9:07









                                                              Ishan Thilina Somasiri

                                                              8381021




                                                              8381021










                                                              answered Jan 7 '18 at 6:00









                                                              JayJay

                                                              1




                                                              1












                                                              • running this in scratchpad gives "today not defined."

                                                                – Nick
                                                                Jan 7 '18 at 6:22

















                                                              • running this in scratchpad gives "today not defined."

                                                                – Nick
                                                                Jan 7 '18 at 6:22
















                                                              running this in scratchpad gives "today not defined."

                                                              – Nick
                                                              Jan 7 '18 at 6:22





                                                              running this in scratchpad gives "today not defined."

                                                              – Nick
                                                              Jan 7 '18 at 6:22

















                                                              draft saved

                                                              draft discarded
















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid


                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.

                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function ()
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15397372%2fjavascript-new-date-ordinal-st-nd-rd-th%23new-answer', 'question_page');

                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                                              Edmonton

                                                              Crossroads (UK TV series)