Clock in Javascript

Lakhangarg
Posted by Lakhangarg under JavaScript category on | Views : 1850
To Show Clock in Javascript (Time Format: SAT 4-25-2009 2:43:22 PM)

Function to create a array of given length:

function MakeArray(size)
{
this.length = size;
for(var i = 1; i <= size; i++)
{
this[i] = "";
}
return this;
}


Function to display time. in this at the end we write setTimeout("showtime()",1000).
this code will call the showtime function after every second.

function showtime ()
{
var now = new Date();
var year = now.getYear();
var month = now.getMonth() + 1;
var date = now.getDate();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var day = now.getDay();
Day = new MakeArray(7);
Day[0]="SUN";
Day[1]="MON";
Day[2]="TUE";
Day[3]="WED";
Day[4]="THU";
Day[5]="FRI";
Day[6]="SAT";
var timeValue = "";
timeValue += (Day[day]) + " ";
timeValue += ((month > 10) ? " 0" : " ") + month + "-";
timeValue += date + "-" + year + " ";
timeValue += ((hours <= 12) ? hours : hours - 12);
timeValue += ((minutes <>
timeValue += ((seconds <>
timeValue += (hours <>
document.getElementById('lblClock').innerHTML = timeValue;
setTimeout("showtime()",1000);
}


Paste the above code in the script tag in Head section and add the following line after the closing of HTML tag:
showtime();

Use a span to show the clock - id of span used in this code is lblClock

Comments or Responses

Login to post response