Search code examples
phpordinal

How to add ordinal suffixes to date script


I have been trying to workout a script to add ordinal suffixes to my date script (seen below), but all attempts to write one or search on here for one has alas failed. Any insight to help fix the script would be greatly appreciated.

https://jsfiddle.net/wq5t4gh7/1/

var months = ["Month1", "Month2", "Month3", "Month4", "Month5", "Month5", "Month7", "Month8", "Month9", "Month10", "Month11", "Month12"];
var n = new Date();
var y = n.getFullYear().toString().substr(2,2);;
var m = n.getMonth();
var d = n.getDate();

document.getElementById("date").innerHTML = "The " + d + " of " + months[m] + ", 52" + y;

Solution

  • Might help

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