Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 776
user1635828 Avatar asked Mar 13 '13 22:03

user1635828


People also ask

How do you use ST nd rd th?

When writing ordinal numbers such as 1st, 2nd, 3rd, etc. you should use the last two letters on the word as it would be if you wrote out the whole word. Below are the ordinal numbers both written out and with digits for 1-20. As you can see, 1st, 2nd, and 3rd use -st, -nd, and -rd, but 4th-20th use -th.

What is new date () in JavaScript?

It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.

How do you install st nd rd?

The rules are as follows: st is used with numbers ending in 1 (e.g. 1st, pronounced first) nd is used with numbers ending in 2 (e.g. 92nd, pronounced ninety-second) rd is used with numbers ending in 3 (e.g. 33rd, pronounced thirty-third)

What is the format of new date ()?

const d = new Date("2015-3-25"); The behavior of "YYYY/MM/DD" is undefined.


2 Answers

Note this works for day numbers from 1 to 31.

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";   } }  // test code  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 = `In two weeks it will be the ${date}<sup>${nth(date)}</sup> of ${month} ${fortnightAway.getFullYear()}`;  // test const dates = [...Array(32).keys()].slice(1).map(i => `${i}${nth(i)}`) console.log(dates.join(", "))
sup {   font-size: x-small }
<span id="date"></span>

Here is a version for any number

const nth = function(d) {   const dString = String(d);   const last = +dString.slice(-2);   if (last > 3 && last < 21) return 'th';   switch (last % 10) {     case 1:  return "st";     case 2:  return "nd";     case 3:  return "rd";     default: return "th";   } }  // test const numbers = [...Array(1225).keys()].map(i => `${i}${nth(i)}`) console.log(numbers.join(", "))
sup {   font-size: x-small }
<span id="date"></span>
like image 143
mplungjan Avatar answered Oct 24 '22 16:10

mplungjan


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] : ''); } 

Update 2020-06-23. The following is a better readable answer of the function above:

const getOrdinalNum = (number) => {   let selector;    if (number <= 0) {     selector = 4;   } else if ((number > 3 && number < 21) || number % 10 > 3) {     selector = 0;   } else {     selector = number % 10;   }    return number + ['th', 'st', 'nd', 'rd', ''][selector]; };   
like image 42
The Martin Avatar answered Oct 24 '22 16:10

The Martin