Lesson 7.1 β Digital Clock Project
π Building a Real-Time Digital Clock Using HTML, CSS & JavaScript
π― Learning Objectives
After completing this lesson, students will be able to:
β Build a Real-Time Digital Clock
β Use JavaScript Date Object
β Display Current Time
β Update Time Automatically
β Use setInterval()
β Create Professional UI Design
β Build Real-World JavaScript Projects
π Introduction
Every computer, smartphone, ATM, and digital device displays time.
Examples:
10:30:45 AM
05:15:20 PM
11:59:59 PM
Today we will create:
π Digital Clock Project
using:
β HTML
β CSS
β JavaScript
π Project Preview
Output:
---------------------
DIGITAL CLOCK
10:25:30 AM
---------------------
The clock will:
β Show Hours
β Show Minutes
β Show Seconds
β Update Every Second
β Run Automatically
π― Concepts Used
| Concept | Used |
|---|---|
| Variables | β |
| Functions | β |
| DOM Manipulation | β |
| Events | β |
| Date Object | β |
| setInterval() | β |
ποΈ Project Files
Create:
index.html
style.css
script.js
π― Step 1 β Create HTML Structure
index.html
<!DOCTYPE html>
<html>
<head>
<title>Digital Clock</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="clock-container">
<h1>
Digital Clock
</h1>
<div id="clock">
00:00:00
</div>
</div>
<script src="script.js"></script>
</body>
</html>
π Understanding HTML
Clock Heading:
<h1>
Digital Clock
</h1>
Clock Display Area:
<div id="clock">
00:00:00
</div>
JavaScript will update this area.
π― Step 2 β Design Clock
style.css
body{
margin:0;
height:100vh;
display:flex;
justify-content:center;
align-items:center;
background:#111;
font-family:Arial;
}
.clock-container{
text-align:center;
background:#222;
padding:40px;
border-radius:15px;
box-shadow:0 0 20px gray;
}
h1{
color:white;
}
#clock{
font-size:50px;
color:#00ff00;
font-weight:bold;
}
π― Expected Design
-------------------------
Digital Clock
10:25:30
-------------------------
Dark background with green clock.
π― Step 3 β Create JavaScript
script.js
function updateClock()
{
let now = new Date();
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
document.getElementById("clock").innerHTML =
hours + ":" + minutes + ":" + seconds;
}
π Understanding Date Object
let now = new Date();
Gets current date and time.
Example:
June 4 2026
10:25:30
π― Getting Hours
now.getHours();
Example Output:
10
π― Getting Minutes
now.getMinutes();
Example Output:
25
π― Getting Seconds
now.getSeconds();
Example Output:
30
π Displaying Time
document.getElementById("clock").innerHTML =
hours + ":" + minutes + ":" + seconds;
Output:
10:25:30
π― Problem
Clock updates only once.
Need automatic updates.
π setInterval()
Used to repeat code continuously.
Syntax:
setInterval(functionName,time);
Example:
setInterval(updateClock,1000);
Meaning:
Run updateClock()
Every 1000 milliseconds
(1 second)
π― Final JavaScript Code
script.js
function updateClock()
{
let now = new Date();
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
document.getElementById("clock").innerHTML =
hours + ":" + minutes + ":" + seconds;
}
setInterval(updateClock,1000);
π Problem
Time may display like:
9:5:2
Not professional.
Need:
09:05:02
π― Adding Leading Zero
if(hours < 10)
{
hours = "0" + hours;
}
if(minutes < 10)
{
minutes = "0" + minutes;
}
if(seconds < 10)
{
seconds = "0" + seconds;
}
π Professional Digital Clock
script.js
function updateClock()
{
let now = new Date();
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
if(hours < 10)
{
hours = "0" + hours;
}
if(minutes < 10)
{
minutes = "0" + minutes;
}
if(seconds < 10)
{
seconds = "0" + seconds;
}
document.getElementById("clock").innerHTML =
hours + ":" + minutes + ":" + seconds;
}
setInterval(updateClock,1000);
updateClock();
π― 12-Hour Clock Format
Convert:
14:30:00
to:
02:30:00 PM
Code:
let period = "AM";
if(hours >= 12)
{
period = "PM";
}
if(hours > 12)
{
hours = hours - 12;
}
π 12-Hour Format Example
document.getElementById("clock").innerHTML =
hours + ":" + minutes + ":" + seconds +
" " + period;
Output:
02:30:15 PM
π» Complete Project Code
index.html
<!DOCTYPE html>
<html>
<head>
<title>Digital Clock</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="clock-container">
<h1>Digital Clock</h1>
<div id="clock"></div>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
body{
margin:0;
height:100vh;
display:flex;
justify-content:center;
align-items:center;
background:#111;
font-family:Arial;
}
.clock-container{
background:#222;
padding:40px;
border-radius:15px;
text-align:center;
box-shadow:0 0 20px gray;
}
h1{
color:white;
}
#clock{
font-size:50px;
font-weight:bold;
color:#00ff00;
}
script.js
function updateClock()
{
let now = new Date();
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
let period = "AM";
if(hours >= 12)
{
period = "PM";
}
if(hours > 12)
{
hours = hours - 12;
}
if(hours < 10)
{
hours = "0" + hours;
}
if(minutes < 10)
{
minutes = "0" + minutes;
}
if(seconds < 10)
{
seconds = "0" + seconds;
}
document.getElementById("clock").innerHTML =
hours + ":" + minutes + ":" + seconds +
" " + period;
}
setInterval(updateClock,1000);
updateClock();
π― Expected Output
Digital Clock
02:45:18 PM
Updates every second automatically.
π« Real World Applications
Digital Clocks are used in:
β Computers
β Mobile Phones
β Railway Stations
β Airports
β Digital Sign Boards
β Smart TVs
β IoT Dashboards
β Robotics Control Panels
β οΈ Common Beginner Mistakes
Mistake 1
Forgetting setInterval()
Clock updates only once.
Mistake 2
Wrong Element ID
document.getElementById("clock")
must match HTML ID.
Mistake 3
Forgetting updateClock()
Without:
updateClock();
clock appears blank initially.
π Best Practices
β Use External CSS
β Use External JavaScript
β Format time properly
β Use meaningful variable names
β Keep code organized
π Project Challenge
Modify the project and add:
β Current Date
β Day Name
β Different Clock Colors
β Background Color Changer
π Practical Activity
Create:
College Digital Clock
Features:
- College Name
- Live Time
- Live Date
- AM/PM Format
π Assignment
Assignment 7.1
- What is a Digital Clock?
- What is the Date Object?
- What does
getHours()do? - What does
setInterval()do? - Create a digital clock project.
- Add AM/PM format.
- Add current date display.
π§ Quiz
Q1. Which object provides current date and time?
A. Time
B. Date
C. Clock
D. Timer
β Answer: B
Q2. Which method gets current hour?
A. getHour()
B. getTime()
C. getHours()
D. getClock()
β Answer: C
Q3. What is the value of 1000 in setInterval?
A. 1000 Minutes
B. 1000 Hours
C. 1000 Milliseconds
D. 1000 Days
β Answer: C
Q4. Which function updates the clock continuously?
A. updateClock()
B. alert()
C. prompt()
D. print()
β Answer: A
Q5. Digital Clock project uses?
A. DOM Manipulation
B. Functions
C. Date Object
D. All of These
β Answer: D
π Lesson Summary
β Built a real-time Digital Clock.
β Learned JavaScript Date Object.
β Used getHours(), getMinutes(), and getSeconds().
β Used setInterval() for automatic updates.
β Created a professional JavaScript project.