Return to Help Center

Welcome to the Bravenet Help Forums. Here you can chat and meet other Bravenet Members. Please do not post advertisements.

Forum: Copy/Paste Code Discussions
Start a New Topic 
  
Author
Comment
Revised code for showing date on web page using JavaScript

The code given on Bravenet for displaying the date is not quite correct. Here is a correct version:

var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','Ocober','November','December');
var d = new Date();
var weekDay = days[d.getDay()];
var month = months[d.getMonth()];
var day = d.getDate();
var year = d.getYear();
var suffix = 'th';
if (day == 1) suffix = 'st';
else if (day == 2) suffix = 'nd';
else if (day == 3) suffix = 'rd';
else if (day == 21) suffix = 'st';
else if (day == 22) suffix = 'nd';
else if (day == 23) suffix = 'rd';
else if (day == 31) suffix = 'st';
if (year < 1000) year = year + 1900;
{document.write(weekDay+', '+day+suffix+' '+month+', '+year);}

Notice the line starting "if(year < 1000 ..."
This is to correct browsers which otherwise would display 2012 as "112", including IE with Windows 8!

I hope this helps. Ray.