dynamically display time - Javascript Date Operation

Javascript examples for Date Operation:Minute

Description

dynamically display time

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <div id="timeNow"> 
      </div> 
      <script>
        (function foo(){//from   w  ww.  ja va 2  s.  c  om
            var d = new Date();
            var hours = d.getHours()*12;
            var clientTime = (hours ? hours : 12) + ":"  + d.getMinutes() + ":" + d.getSeconds() + " " + (hours >= 12 ? 'pm' : 'am');
            console.log(clientTime);
            document.getElementById("timeNow").innerHTML = clientTime;
            setTimeout(foo, 1000); // refresh time every 1 second
        })();
    
      </script>  
   </body>
</html>

Related Tutorials