So, today we will be creating Digital using HTML, CSS and JavaScript.
Digital Clock will look like this and it will show you the current time in 24-hours format. Check your current time. Does it match?
:
:
Step 2- Open the folder in VS Code and create three files namely index.html, style.css and index.js. (You can also name your files according to your wish) You can also refer to the images shown here.
Let's understand the HTML Code first:
![]() |
Click on the image to view |
For more read this - What is CSS ? | Some Basic Properties
in CSS to get started
To link the CSS file use- <link rel="stylesheet" href="style.css">
To link JS file use- <script src="index.js"></script>
- Remember: ID is unique and can be assigned to a
particular tag/element, whereas class can be assigned to more than one
tag/element.
Step 3- The 'span' tag is an inline element and can be easily styled by CSS and manipulated with JavaScript. Notice in the image, that we have added : , before the span tag, just to separate the hours, minutes and seconds from each other.
Here, we are completed with our HTML code.
Let's move to the CSS Code:
// .container{
text-align: center;
margin-top: 50px;
color: blue;
font-size: 25px;
}
![]() |
Click on the image to view |
Step 1- In JavaScript we have two methods :
1). setInterval - It allows us to run
a function repeatedly, after regular interval of time.
The syntax of setInterval is : setInterval(function_name, time)
It takes two arguments, the name of function and time in milliseconds.
2). setTimeout - It allows us to run a
function only once after the interval of time. The syntax of
setTimeout is similar to setInterval: setTimeout(function_name, time)
Here, we will be using Set Interval method, since we want our time to be
updated continuously after every second or 1000 millisecond.
Step 2- Now, we create a function named as 'update'. This function
will be used to update the time after every second.
Step 3- Let's move inside the function part of the
JavaScript.
=> let is a keyword which is
used in JS to identify the variables.
Here, we have created four variables, time, hrs, min and sec. Let's understand
the use of each variable briefly.
=> let time = new Date();
Here, Date objects are created with the new Date() constructor. 'new
Date()' creates a new date object with the current date and time and assign the
value to the variable 'time'.
=> let hrs=time.getHours();
.getHours() method returns the current hours of a date.
Similarly, the .getMinutes() and .getSeconds() methods,
returns the minutes and seconds of the current time respectively.
=> To access or manipulate the content/elements of HTML, we use DOM
(Document Object Model) methods such as .getElementById or
.getElementsByClassName.
- Syntax: document.getElementById('name_of_ID')
Now, we are done with creating the function. We pass the function name to the 'setInterval' method and specify the time as 1000(in milliseconds).
Finally, your Digital Clock is ready to show the time.




0 Comments
If you have any doubts, please let me know.