1

I have the following Javascript to display the date on a page, however I wanted to add in the "th", "st" and "rd" suffixes after each date number (ie. "6th").

Is there a way of doing this with the following code I currently have?

    <script language="Javascript">
<!-- 

// Array of day names
var dayNames = new Array("You're working today? It's Sunday","Hello, it's Monday","Hello, it's Tuesday","Hello, it's Wednesday",
    "Hello, it's Thursday","Hello, it's Friday","You're working today? It's Saturday");

// Array of month Names
var monthNames = new Array(
"January","February","March","April","May","June","July",
"August","September","October","November","December");

var now = new Date();
document.write (dayNames[now.getDay()] + ", " + 
now.getDate() + " " + 
monthNames[now.getMonth()] + " " + 
now.getFullYear() + ".");
// -->
</script>

Thanks in advance

head_bigg
  • 241
  • 1
  • 2
  • 8

2 Answers2

0

There are other solutions if you don't want to use a library. Doing a quick search on StackOverflow led me to this answer. You'll need to incorporate one of these functions into your code taking the value from now.getDate().

Community
  • 1
  • 1
LeDoc
  • 935
  • 2
  • 12
  • 24
0

You are using dayNames[now.getDay()] suggests that the key you are searching for is now.getDay(), which is not zero-based. I suggest that you move your value for Sunday in the end of the array and use dayNames[now.getDay() - 1] for more clarity.

On the subject, you can check this answer for the solution: Convert date day (05/12/2011 to 12th)

Community
  • 1
  • 1
victor175
  • 624
  • 3
  • 10
  • 23