Live Clock using javascript

Many times we will be needed to display the live clock ticking in the applications which will be running on client side. For this purpose just use the javascript function below which has been well tested in many applications.

 

<SCRIPT language="JavaScript">
<!--

function showclock()
{
var thetime=new Date();

var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var nday=thetime.getDay();
var nmonth=thetime.getMonth();
var ntoday=thetime.getDate();
var nyear=thetime.getYear();
var AorP=" ";

if (nhours>=12)
    AorP="PM";
else
    AorP="AM";

if (nhours>=13)
    nhours-=12;

if (nhours==0)
   nhours=12;

if (nsecn<10)
 nsecn="0"+nsecn;

if (nmins<10)
 nmins="0"+nmins;

if (nday==0)
  nday="Sun";
if (nday==1)
  nday="Mon";
if (nday==2)
  nday="Tues";
if (nday==3)
  nday="Wed";
if (nday==4)
  nday="Thur";
if (nday==5)
  nday="Fri";
if (nday==6)
  nday="Sat";

nmonth+=1;

if (nyear<=99)
  nyear= "19"+nyear;

if ((nyear>99) && (nyear<2000))
 nyear+=1900;

document.clockform.clockspot.value=nhours+":"+nmins+":"+nsecn+" "+AorP+" "+nday+", "+nmonth+"/"+ntoday+"/"+nyear;

setTimeout('showclock()',1000);

}

//-->
     </SCRIPT>

No comments:

Post a Comment