JavaScript - Simple Display Live Clock

In this tutorial we will create a Simple Display Live Clock using JavaScript. This code will automatically generate and display a gui of clock when the user click the launch button. The code itself use a setInterval function a built-in javascript function that loop any method that have been declare in any interval value in order to repeat the already set properties to achieve a clock like tool. You can apply this script to your code, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user. .

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Simple Display Live Clock</h3>
  15. <hr style="border-top:1px dottec #ccc;">
  16. <div class="col-md-2"></div>
  17. <div class="col-md-8">
  18. <button class="btn btn-primary" onclick="generateTime();">Launch Clock</button>
  19. <br /><br />
  20. <div id="displayClock" style=></div>
  21. </center>
  22. </div>
  23. </div>
  24. </body>
  25. <script src="js/script.js"></script>
  26. </html>

Creating the Script

This code contains the script of the application. This code will display a clock when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. function generateTime(){
  2. var date = new Date();
  3. var hour = date.getHours();
  4. var minute = date.getMinutes();
  5. var seconds = date.getSeconds();
  6. var day = "AM"
  7.  
  8. if(hour == 0){
  9. hour = 12;
  10. }
  11.  
  12. if(hour > 12){
  13. hour-=12;
  14. day = "PM"
  15. }
  16.  
  17. hour = (hour < 10) ? "0" + hour : hour;
  18. minute = (minute < 10) ? "0" + minute : minute;
  19. seconds = (seconds < 10) ? "0" + seconds : seconds;
  20.  
  21. var currentTime = hour + ":" + minute + ":" + seconds + " " + day;
  22. document.getElementById("displayClock").innerHTML = "<label style='font-size:60px; color:green; border:5px dotted #000; padding:20px;'>"+currentTime+"</label>";
  23.  
  24. setInterval(generateTime, 1000);
  25. }
  26.  
  27.  
There you have it we successfully created a Simple Display Live Clock using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment