Click here to Skip to main content
15,886,199 members
Articles / Web Development / HTML
Article

Good one and simple example for beginners to display time on browser using java script

Rate me:
Please Sign up or sign in to vote.
1.50/5 (6 votes)
30 Sep 2005 24.3K   212   5  
The code demonstrates displaying time using javascript.The javascript function showtime will be called onLoad event of form body tag.settTimeout will keep on dispalying the time for set interval.

Sample Image - JavascriptTime.jpg

Introduction

<script language="javascript">
   function showTime()
   {
    now=new Date();
    var hour=now.getHours();
    var min=now.getMinutes();
    var sec=now.getSeconds();

    if (min<=9) { min="0"+min; }
    if (sec<=9) { sec="0"+sec; }
    if (hour<10) { hour="0"+hour; }  
      
       
    if (hour>12)
     { hour1= hour - 12;}
    else
     hour1 = hour;
    if (hour1<10) { hour1="0"+hour1; } 
    
    var ctime =  hour1 + ":" + min + ":" + sec;
    LABEL1.innerText= ctime;
    setTimeout("showTime()", 1000);
   }
  </script>

 

The above code diplayes the time in a label control named LABEL1.
you can create a simple html page and add a label and name it LABEL1.
Then add the above script into header section of the html.
Try accessing the page. You can also down load the sample code.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
fgdf

Comments and Discussions

 
-- There are no messages in this forum --