0

I checked the other posts similar to this but I didn't get the answer I needed.

How do I add those words to the end of my numbers?

here's my jQuery code:

var monthNames = [ "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December" ];
        var dayNames= ["Sunday,","Monday,","Tuesday,","Wednesday,","Thursday,","Friday,","Saturday,"]

        var newDate = new Date();
        newDate.setDate(newDate.getDate());    
        $('#the-date').html(dayNames[newDate.getDay()] + " " + monthNames[newDate.getMonth()] + ' ' + newDate.getDate() + ' ' );
Chad
  • 89
  • 9

2 Answers2

0

moment.js is great for working with dates and offers this via format. For example:

moment().format("MMM Do YY"); // May 20th 16
Chris
  • 149
  • 10
0

From this question:

var suffix = "";
switch(inst.selectedDay) {
    case '1': case '21': case '31': suffix = 'st'; break;
    case '2': case '22': suffix = 'nd'; break;
    case '3': case '23': suffix = 'rd'; break;
    default: suffix = 'th';
}

Add in like this:

// below `newDate.setDate` line
var dateNumber = newData.getDate();
var suffix = "";
switch(dateNumber) {
    case '1': case '21': case '31': suffix = 'st'; break;
    case '2': case '22': suffix = 'nd'; break;
    case '3': case '23': suffix = 'rd'; break;
    default: suffix = 'th';
}
$('#the-date').html(dayNames[newDate.getDay()] + " " + monthNames[newDate.getMonth()] + ' ' + dateNumber + suffix + ' ' );
Community
  • 1
  • 1
Sander Moolin
  • 450
  • 4
  • 11
  • And if the strings in the cases don't work, use ints: `case 1: case 21: case 31:` etc. (the same as the code above, just without the quotations surrounding the numbers. – Sander Moolin May 20 '16 at 19:59