How to Display Live Digital Clock in JavaScript

How to Display Live Digital Clock in JavaScript

Introduction

In this tutorial we will create a How to Display Live Digital Clock in JavaScript. This tutorial purpose is to teach you how to implement a live digital clock into your website. This will tackle all the basic function that will display the digital clock. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any application or system if you want to display a digital clock in your page. I will give my best to provide you the easiest way of creating this program Digital Clock. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface to our application. This code will only display the digital clock and buttons. To create this simply copy and write it into your text editor, then save it 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">How to Display Live Digital Clock in JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;">
  16. <div class="col-md-2"></div>
  17. <div class="col-md-8">
  18. <center><button class="btn btn-lg btn-default" onclick="generateClock(this);">Start Clock</button></center>
  19. <br /><br />
  20. <center>
  21. <div id="displayClock" style=></div>
  22. </center>
  23. </div>
  24. </div>
  25. </body>
  26. <script src="script.js"></script>
  27. </html>

Creating JavaScript Function

This is where the main function of the application is. This code launch the digital clock when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function generateClock(btn){
  2. let date = new Date();
  3. let hour = date.getHours();
  4. let minute = date.getMinutes();
  5. let seconds = date.getSeconds();
  6. let 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. let currentTime = hour + ":" + minute + ":" + seconds + " " + day;
  22. document.getElementById("displayClock").innerHTML = "<div style='font-size:60px; color:green; border-radius:10px; border:5px dashed #000; padding:20px;'>"+currentTime+"</div>";
  23.  
  24. setInterval(generateClock, 1000);
  25. btn.style.display="none";
  26. }
  27.  
  28.  

In the code above we create just created only one method called generateClock(), this function is consist of multiple variables conditional statement. To create our digital clock we must first create multiple variables, this will serve has the breakdown for the time element. We simply use a conditional statement to get the the accurate conversion of the time we will be displaying. After that we will use setInterval() function to create a timer for the duration of the clock, and lastly we will append time value into the HTML page

Output:

The How to Display Live Digital Clock in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Display Live Digital Clock in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment