Javascript Date format day with suffix, 1st, 2nd, 3rd, th

Description

Javascript Date format day with suffix, 1st, 2nd, 3rd, th


let months = ["January", "February", "March", "April", "May", "June", "July",
              "August", "September", "October", "November", "December"];

let dateNow = new Date();
let yearNow = dateNow.getFullYear();
let monthNow = months[dateNow.getMonth()];
let dayNow = dateNow.getDate();
let daySuffix;/*from   w  w w . j a  v  a 2 s.  c  o m*/

switch (dayNow) {
    case 1:
    case 21:
    case 31:
        daySuffix = "st";
        break;
    case 2:
    case 22:
        daySuffix = "nd";
        break;
    case 3:
    case 23:
        daySuffix = "rd";
        break;
    default:
        daySuffix = "th";
        break;
}

console.log("It is the " + dayNow + daySuffix + " day ");
console.log("in the month of " + monthNow);
console.log(" in the year " + yearNow);



PreviousNext

Related