The code that puts the date and time on the index page

Back

A few people asked about this code so here it is.

 

//Code written by Nigel Gibson - Use it with my blessing :-)

var now = new Date(); //Declares a variable called now. This is a date number using the built in function Date()
var hours = now.getHours(); //Use the built in method getHours to set the hour variable to the hour number from now
var minutes = now.getMinutes(); //Use the built in method getMinutes to set the minute variable to the hour number from now

var dayname = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); //declares an array called dayname - this array has 7 elements
var monthname = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); //declares and array called monthname
hours += (((minutes < 10) ? ":0" : ":") + minutes) //Assembles the hour and minute value into one lump. Inserts a leading 0 if the minute value is less than 10

var thisday
mydate = new Date()
myday = mydate.getDay()
mymonth = mydate.getMonth()
myweekday= mydate.getDate()
var myyear= mydate.getYear()
thisday = dayname[myday] //sets thisday to the value of the element in the dayname array accessed by the myday value. Thus if myday is 0 thisday is element 0 in the array (Sunday). JavaScript uses zero based arrays so the first element is at position zero.
if (myyear<1900)
myyear=myyear+1900 //Navigator reads the year number (getYear) from 1900 so this adds 1900 if myyear is less than 1900. As IE reads getYear differently it jumps this bit

if((myweekday==1)||(myweekday==31)||(myweekday==21))
var text ="st"
else if((myweekday==2)||(myweekday==22))
var text ="nd"
else if((myweekday==3)||(myweekday==23))
var text ="rd"
else
var text="th"
//The next part writes the stuff out to the HTML page. You'll see that tags are written as well as the variable values.
document.write("<P ALIGN=center><FONT FACE=VERDANA,ARIAL title=Clock><hr color=steelblue><b>" + hours + " - " + thisday +", "+ monthname[mymonth]+" "); document.write(myweekday +""+ text + ", "+ myyear + "</B><hr color=steelblue>");

 

 

Copy and paste this into the body of a web page or, save it as a JS script and call it as required.

Nogbad - click here to go back to the index page