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

javascript ordinal suffix
"st" | "nd" | "rd" | "th" date
javascript date format
javascript string to date
"st" | "nd" | "rd" | "th" date in word
javascript date output format
javascript convert date format
node js date format

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!

Here:

JSFiddle

const nth = function(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";
  }
}
const fortnightAway = new Date(+new Date + 12096e5);
const date = fortnightAway.getDate();
const month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][fortnightAway.getMonth()];

document.getElementById("date").innerHTML = `${date}<sup>${nth(date)}</sup> ${month} ${fortnightAway.getFullYear()}`;
sup {
  font-size: x-small
}
<span id="date"></span>

JavaScript: Get English ordinal suffix for the day of the month , Write a JavaScript function to get English ordinal suffix for the day of the month, 2 characters (st, nd, rd or th.). Test Data : dt= new Date(); Often times developers' new to a platform end up reinventing the wheel; implementing logic already provided (talking from experience). There is no out-of-box solution for adding those suffixes (st, nd, rd, th), however, in your case you could have leveraged the Utilities.formatDate() method to make things a little easier:

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) || n % 10 > 3 ? 0 : n % 10] : '');
}

Format date with nth, st, rd, th - JSFiddle, Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code case 2: return "nd";. 6 const fortnightAway = new Date(+new Date + 12096e5);. JavaScript nouvelle Date Ordinal (st, nd, rd, th) si possible, sans bibliothèques JavaScript ou beaucoup de code clunky, je cherche la façon la plus simple de formater une date dans deux semaines dans le format suivant:

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';
    }
}

Javascript Customised Dates, Once you have a Date object you have quite a lot of inbuilt properties available to you. These allow you to Like this: var the_date = new Date(); (An ordinal is the st, nd, rd, or th at the end of the number, like 1st, 2nd 3rd, 4th.) The function  Ordinal (st, nd, rd, th) dates in javascript. JavaScript / Ajax / DHTML Forums on Bytes.

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) {
    if (dom == 31 || dom == 21 || dom == 1) return dom + "st";
    else if (dom == 22 || dom == 2) return dom + "nd";
    else if (dom == 23 || dom == 3) return dom + "rd";
    else return dom + "th";
};

or compact version using conditional operators

function dateOrdinal(d) {
    return d+(31==d||21==d||1==d?"st":22==d||2==d?"nd":23==d||3==d?"rd":"th")
};

http://jsben.ch/#/DrBpl

Date Ordinal (st, nd, rd, th) in Angular?, you can build the custom date filter to add the suffix rd, th and st. The custom <​script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"><​/script> <! Comfort Express Inc Charter Bus in New York City. Re: Date format of just "st" "nd" "rd" and "th" A formula cannot affect the format of a cell, so you can't have part of it in superscript (without VBA). Anyway, you shouldn't ask a question in someone else's thread - start your own (see Forum Rules at the top of the screen).

Lots of answers, here's another:

function addOrd(n) {
  var ords = [,'st','nd','rd'];
  var ord, m = n%100;
  return n + ((m > 10 && m < 14)? 'th' : ords[m%10] || 'th');
}

// 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

Ordinal (st, nd, rd, th) dates in javascript, How could I do that with this small bit of javascript code? <script language = "​JavaScript"> var now = new Date(); var monNames = new. Array("  JavaScript new Date Ordinal (st, nd, rd, th) Ask Question which I found the most simplest way to get this date Ordinal generated for given JavaScript date:

numberToOrdinal conversion - JavaScript, Finish the function “numberToOrdinal”, which should take a number and return it as a string with the correct ordinal indicator suffix (in English). 12) { return n + "​nd"; } if (j == 3 && k != 13) { return n + "rd"; } return n + "th"; } We accomplish this by creating thousands of videos, articles, and interactive coding  Row 2 uses the formula from the eBook (PDF format) to display today’s date with the th. As you can see, adding 1 to today’s date does not work here because 1 is being added to a text string. This is why I was asked how to format dates to include st, nd, rd and th. Format dates to include st, nd, rd and th

PHP code to add st, nd, rd or th to a number, PHP code to add st, nd, rd or th to a number function ordinal($input_number) Add new comment of code, this time in JavaScript, which will output the date that a page was last Initialise variables: var fulldate = new Date(document. Add ST/ND/RD/TH to DAY in DATE If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

can i insert a legal style of date (), When I use the format 'ddd day of mmmmm yyyy' I get: 30 30ay of August 2015 Select "JavaScript" on the left, then drill down further to find the description of all Also I would use a function to add the ordinal suffix to the date, that function could var aSuffix = new Array("th", "st", "nd", "rd"); // only need to evaluate values 0  JavaScript exercises, practice and solution: Write a JavaScript function to get English ordinal suffix for the day of the month, 2 characters (st, nd, rd or th.).

Comments
  • apart from a missing "of" I have no problem with that date
  • try this stackoverflow.com/questions/1056728/…
  • 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
  • Works perfect... +1 for d>3 && d<21
  • 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";
  • That is missing 21,22,23
  • if (d == 0) return ''; ... if used outside of a date context
  • Sorry? Not sure what you mean
  • The if is for teens, so 13 is 13th and not 13rd.
  • Why the this%1? Mod 1 is always 0 so the statement will always fail, no?
  • It's just a lot simpler solution. It will check the last number in day and then check if it exists in the object. If it does then return the correct suffix and then if not return th.
  • Unfortunately this method is too simple and doesn't work for days like the 21st.
  • I found an issue with this, 13 = 13rd
  • running this in scratchpad gives "today not defined."