Emmanuel Akinfulubi

JavaScriptDate

Format a date with ordinal day (1st, 21st) in JavaScript

Intermediate · 4 min read

Invitations, blog posts and receipts often write dates like "15th January 2026" or "1st February". The little ending after the number, "st", "nd", "rd" or "th", is called an ordinal suffix. It turns "one" into "first" and "two" into "second".

In this tutorial you will write ordinal(n), which adds the right suffix to a number, and withOrdinal(date), which uses it to write a whole date. The rule looks simple, but 11, 12 and 13 break it, and that is what this exercise is really about. You need to know the % remainder operator and how to read the day and month from a date.

The pattern, and its exception

Look at the last digit of the number:

  • Ends in 1: "st" (1st, 21st, 31st)
  • Ends in 2: "nd" (2nd, 22nd)
  • Ends in 3: "rd" (3rd, 23rd)
  • Anything else: "th" (4th, 10th, 15th)

The exception is the teens. We say "eleventh", "twelfth" and "thirteenth", so 11, 12 and 13 all take "th", even though their last digits are 1, 2 and 3.

Getting the last digits with %

% gives the remainder after dividing. Dividing by 10 leaves the last digit, and dividing by 100 leaves the last two:

JavaScript

console.log(21 % 10);   // 1
console.log(13 % 10);   // 3
console.log(113 % 100); // 13

Here is the exercise. Try it on your own first if you like, then read on and we will build it step by step.

44 Ordinal day numbers: 1st, 2nd, 3rd, 11th, 21st

fair

Add the English ordinal suffix to a day number, then use it in a date. 11, 12 and 13 do not follow the pattern the last digit suggests.

Stuck? Three hints, smallest first
  1. 1. The last digit gives st, nd, rd, th — except in the teens.
  2. 2. Check the last two digits for 11 to 13 first, and return "th" for those before looking at the last digit.
  3. 3. Test 11, 12 and 13 explicitly. They are the whole exercise.

Step 1: follow the last digit

A switch checks one value against several cases. Here the value is the last digit:

Step 1

function ordinalFirstTry(n) {
  switch (n % 10) {
    case 1: return n + 'st';
    case 2: return n + 'nd';
    case 3: return n + 'rd';
    default: return n + 'th';
  }
}
console.log([1, 2, 3, 4, 21].map(ordinalFirstTry).join(' ')); // 1st 2nd 3rd 4th 21st

Each case returns straight away, so there is no need for break. default catches every other digit. map runs the function on each number, and join(' ') prints them on one line.

Step 2: the wrong turn, the teens

Now try 11, 12 and 13:

Step 2

console.log([11, 12, 13].map(ordinalFirstTry).join(' ')); // 11st 12nd 13rd

"11st" is wrong. The last digit is 1, so the first case matched. We need to spot the teens before looking at the last digit.

Step 3: check the last two digits first

Step 3

function ordinal(n) {
  const lastTwo = n % 100;
  if (lastTwo >= 11 && lastTwo <= 13) return n + 'th';
  switch (n % 10) {
    case 1: return n + 'st';
    case 2: return n + 'nd';
    case 3: return n + 'rd';
    default: return n + 'th';
  }
}
console.log([11, 12, 13].map(ordinal).join(' ')); // 11th 12th 13th
console.log([21, 22, 23].map(ordinal).join(' ')); // 21st 22nd 23rd
console.log(ordinal(111));                        // '111th'

The if returns "th" for any number whose last two digits are 11, 12 or 13. Everything else falls through to the switch as before.

Why n % 100 and not just n === 11 || n === 12 || n === 13? Because 111 and 112 follow the same rule: "one hundred and eleventh". The remainder check handles them for free. Days of the month never go past 31, but you may reuse this helper for positions in a race or rows in a table.

Step 4: use it in a date

Now add the month name and the year. The month list is in calendar order, so getMonth(), which counts from 0, picks the right name:

Step 4

const MONTH_NAMES = [
  'January', 'February', 'March', 'April', 'May', 'June',
  'July', 'August', 'September', 'October', 'November', 'December',
];
const withOrdinal = (date) =>
  ordinal(date.getDate()) + ' ' + MONTH_NAMES[date.getMonth()] + ' ' + date.getFullYear();
console.log(withOrdinal(new Date(2026, 0, 15))); // '15th January 2026'
console.log(withOrdinal(new Date(2026, 1, 1)));  // '1st February 2026'

The day comes first, then the month, then the year. There is no leading zero, so you get "1st", not "01st".

Step 5: check a whole month

A quick way to trust the rule is to try every day from 1 to 31 and keep the ones ending in "st":

Step 5

const days = Array.from({ length: 31 }, (unused, i) => i + 1);
console.log(days.filter((n) => ordinal(n).endsWith('st')).join(' ')); // 1 21 31

Array.from({ length: 31 }, ...) builds the list 1 to 31. Only 1, 21 and 31 take "st". 11 is correctly missing.

The full solution

The panel has ordinal and withOrdinal. It prints 1 to 5, the teens, the twenties and a full date. The checks cover the teens, 31, 111, two dates, and the whole month. Press Run to see them.

The answer

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

function ordinal(n) {
  const lastTwo = n % 100;
  if (lastTwo >= 11 && lastTwo <= 13) return n + 'th';   // eleventh, twelfth, thirteenth

  switch (n % 10) {
    case 1: return n + 'st';
    case 2: return n + 'nd';
    case 3: return n + 'rd';
    default: return n + 'th';
  }
}

const withOrdinal = (date) =>
  ordinal(date.getDate()) + ' ' + MONTH_NAMES[date.getMonth()] + ' ' + date.getFullYear();

console.log('1 to 5  :', [1, 2, 3, 4, 5].map(ordinal).join(' '));
console.log('11 to 13:', [11, 12, 13].map(ordinal).join(' '), '<- the exception');
console.log('21 to 23:', [21, 22, 23].map(ordinal).join(' '), '<- back to the pattern');
console.log('15/01/2026 ->', withOrdinal(new Date(2026, 0, 15)));

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('the first three follow the last digit', [1, 2, 3].map(ordinal), ['1st', '2nd', '3rd']);
test('four onwards is th', [4, 5, 6].map(ordinal), ['4th', '5th', '6th']);
test('the teens break the pattern', [11, 12, 13].map(ordinal), ['11th', '12th', '13th']);
test('but the twenties restore it', [21, 22, 23].map(ordinal), ['21st', '22nd', '23rd']);
test('the last day of a long month', ordinal(31), '31st');
test('checking the last two digits also handles 111', ordinal(111), '111th');
test('a date, day first', withOrdinal(new Date(2026, 0, 15)), '15th January 2026');
test('with no leading zero on a single-digit day', withOrdinal(new Date(2026, 1, 1)), '1st February 2026');
test('across a month, only 1, 21 and 31 take st', Array.from({ length: 31 }, (unused, i) => i + 1).filter((n) => ordinal(n).endsWith('st')), [1, 21, 31]);

n % 10 === 1 ? 'st' is right for 1, 21 and 31 and wrong for 11, which is why the last-two-digits check has to come first. Testing the modulo 100 rather than a literal 11 || 12 || 13 costs nothing and stays correct for 111 and 112, which matters the moment you use the same helper for anything other than a day of the month.

Suffixes like this are English-only. There is no locale-independent way to build them by hand, which is a good argument for keeping ordinals out of any string that has to be translated.

Common mistakes

  • Checking the last digit first. 11, 12 and 13 come out as "11st", "12nd" and "13rd". Test for the teens before the switch.
  • Testing only 1 to 10. Everything looks right until the 11th. Always try 11, 12, 13 and 21.
  • Building ordinals into text that will be translated. These suffixes are English only. Other languages write ordinals differently, so keep them out of text that has to be translated.

Try it yourself

  1. Use ordinal to print race positions for the first 25 runners: "1st place", "2nd place", and so on.
  2. Write ordinalWord(n) that returns only the suffix, so ordinalWord(22) is 'nd'.
  3. Change withOrdinal to write "Thursday 15th January 2026" by adding the weekday name.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.