Course Content
Project file
0/1
Complete Web Designing Course for Beginners

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

  1. What is a Digital Clock?
  2. What is the Date Object?
  3. What does getHours() do?
  4. What does setInterval() do?
  5. Create a digital clock project.
  6. Add AM/PM format.
  7. 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.

Scroll to Top