TextClock

TextClock Example

Description of TextClock

A live clock implemented by JavaScript.

Code for TextClock

Include following JavaScript code on the page:

function clockTick() 
{
	var months=new Array(13);
	months[1]="January";
	months[2]="February";
	months[3]="March";
	months[4]="April";
	months[5]="May";
	months[6]="June";
	months[7]="July";
	months[8]="August";
	months[9]="September";
	months[10]="October";
	months[11]="November";
	months[12]="December";

	var digital = new Date();
	var hours = digital.getHours();
	var minutes = digital.getMinutes();
	var seconds = digital.getSeconds();
	var da = digital.getDate();
	var mo = months[digital.getMonth()+1];
	var ye = digital.getYear();
    	
	var amOrPm = "AM";
	if (hours > 11) amOrPm = "PM";
	if (hours > 12) hours = hours - 12;
	if (hours == 0) hours = 12;
	if (minutes <= 9) minutes = "0" + minutes;
	if (seconds <= 9) seconds = "0" + seconds;
	dispTime =  da + " " + mo + " " + ye + " @ " + hours + ":" + minutes + ":" + seconds + " " + amOrPm;

	if (document.getElementById("clockid"))
	{
		document.getElementById("clockid").innerHTML = dispTime;
		setTimeout("clockTick()", 1000);
	}
}

Then call the clockTick() function on the page load event (or $( document ).ready event in jQuery)

Finally add a div / span / p tag with an id of clockid on the page.

The live clock should now be ready to run.

Version History

Version Date Description
1.0 27th August 2007 Page created
2.0 29th November 2020 Fixed issue with year format
Text and layout of page updated

Comments For This Page

hi there my name is haillie shaw
By haillie shaw on 4th January 2017
For some reason, it says that the year is 110.
By Your Name Here on 28th June 2010
Thank you. this is better than all the other clocks ive looked at.
By Danmw1 Ltd. on 11th January 2009

Add your own comment below and let others know what you think: