在网页上显示系统时间

来源:互联网 发布:网络常见端口号 编辑:程序博客网 时间:2024/05/01 16:00


<html>
 <head>
  <title>
         Show the current time
  </title>
  <script>
      var temp = null;
   var timeValue = null;
   function divCon()
   {
           temp = document.getElementById('thetime');
           temp.innerText = "Time: " + timeValue;

   }
    var timerID = null;
    var timerRunning = false;
   function stopclock()
   {
       if(timerRunning)
       {
            clearTimeout(timerID);
        }
        timerRunning = false;
   }
   function startclock()
   {
       stopclock();
       showtime();
   }

   function showtime()
   {
         var now = new Date();                                 //Get the current time                                                                                                                                                                                       
        var hours = now.getHours();                           //Get the current hours
        var minutes = now.getMinutes();                       //Get the current minutes
        var seconds = now.getSeconds();                       //Get the current seconds
        timeValue = "" +((hours>12)?"PM ":"AM ");             //以12?制?示系???
        timeValue += ((hours>12)?hours-12:hours);
        timeValue += ((minutes<10)?":0":":") + minutes;    //以双位显示
        timeValue += ((seconds<10)?":0":":") + seconds;    //以双位显示
        divCon();
        timerID = setTimeout("showtime()",1000);              //间隔1000毫秒,即1秒
        timerRunning = true;
   }
  </script>
 </head>
 <body onload="divCon();startclock()">
   <center>
     <h1>TIME</h1>
  <form name = "clock" id = "clock">
        <div  name="thetime" id ="thetime"></div>
  </form>
   </center>
 </body>
</html>