JavaScript - Simple Display Live Clock
Submitted by razormist on Sunday, May 26, 2019 - 19:24.
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.
.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dottec #ccc;">
- <div class="col-md-8">
- <center>
- </center>
- </div>
- </div>
- </body>
- </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- function generateTime(){
- var date = new Date();
- var hour = date.getHours();
- var minute = date.getMinutes();
- var seconds = date.getSeconds();
- var day = "AM"
- if(hour == 0){
- hour = 12;
- }
- if(hour > 12){
- hour-=12;
- day = "PM"
- }
- hour = (hour < 10) ? "0" + hour : hour;
- minute = (minute < 10) ? "0" + minute : minute;
- seconds = (seconds < 10) ? "0" + seconds : seconds;
- var currentTime = hour + ":" + minute + ":" + seconds + " " + day;
- document.getElementById("displayClock").innerHTML = "<label style='font-size:60px; color:green; border:5px dotted #000; padding:20px;'>"+currentTime+"</label>";
- setInterval(generateTime, 1000);
- }
Add new comment
- 438 views