Press ESC to close

Table of Contents

How to create Digital Clock using HTML CSS and JavaScript

How to create Digital Clock using HTML CSS and JavaScript

Dive into building a digital clock for your webpage! This step-by-step guide will use JavaScript to craft a simple clock that displays the current time.We’ll kick things off with fundamental HTML for the document structure and some CSS for styling. Then, JavaScript takes the stage to grab the current time and keep the clock ticking every second.By the end, you’ll have a cool digital clock ready to use on your website.

This is a great project for both beginners and seasoned developers who want to explore JavaScript’s core functions like the Date object and setInterval(). So, fire up your favorite code editor and let’s build!

This project caters to both JavaScript newbies and veterans! Want to learn more about the language and add an interactive element to your website? This digital clock is your perfect practice. Just grab your favorite code editor and let’s dive in!

Read also, How to add style on contact form 7 / Automatic Table of Contents in WordPress Website without plugin.

Preview

Key Features:

Modern Design: Our digital clock boasts a clean and contemporary design that enhances the look of any website.

Real-Time Updates: Stay synchronized with your audience by displaying the current time accurately, ensuring everyone stays on track.

Customization Options: Tailor the clock’s appearance to match your website’s branding and layout preferences effortlessly.

12-Hour or 24-Hour Format: Choose between 12-hour and 24-hour time formats to suit your audience’s preferences and geographical location.

Comprehensive Display: In addition to timekeeping, our clock also showcases the current date, providing visitors with valuable information at a glance.

Integration with WordPress: You can place this digital clock in WordPress widget sidebar or in post.

Custom Coding

To build a digital clock using Visual Studio, we begin by crafting three essential files: index.html for structuring the HTML content, styles.css for styling the elements with CSS, and script.js for implementing the clock’s functionality using JavaScript. All of these files are stored in a single folder to maintain their connection and ensure smooth collaboration. Below, you’ll find the source code for the digital clock.

HTML
CSS
JavaScript
<html> <head> <title>Sample Page</title> <link rel=”stylesheet” href=”styles.css”> </head> <body> <div class=”container”> <div class=”clock”> <div id=”time” class=”time”></div> <div id=”ampm” class=”ampm”></div> <div id=”seconds” class=”seconds”></div> <div id=”date” class=”date”></div> </div> </div> <script src=”script.js”></script> </body> </html>
.container { display: flex; justify-content: center; align-items: center; width: 100%; background-color: #1a1a1a; /* Black background */ } .container { border-radius: 20px; top: 50%; font-size: 3px; padding: 5px; box-shadow: 0 0 20px #5194eb; /* Blue glowing effect */ animation: glow 20s ease-in-out infinite alternate; /* Glowing animation */ background: linear-gradient(to bottom, #3237, #241515); /* Adjust colors as needed */ } @font-face { font-family: ‘Digital-7 mono’; src: url(‘http://nbtechpro.com/wp-content/uploads/2024/03/digital-7-mono.ttf’) format(‘truetype’); /* Adjust the path to the font file */ } .clock { font-size: 16em; text-align: center; font-family: ‘Digital-7 Mono’, sans-serif; /* Use Digital-7 font */ } .time { color: rgb(255, 255, 255); } .seconds { color: rgb(255, 255, 255); } .date { color: rgb(255, 255, 255); font-family: ‘Digital-7 Mono’, sans-serif; }
function updateTime() { const now = new Date(); let hour = now.getHours(); const minute = String(now.getMinutes()).padStart(2, ‘0’); const second = String(now.getSeconds()).padStart(2, ‘0’); const day = String(now.getDate()).padStart(2, ‘0’); const monthNames = [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’, ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’, ‘Dec’]; const month = monthNames[now.getMonth()]; const year = now.getFullYear(); let ampm = hour >= 12 ? ‘PM’ : ‘AM’; hour = hour % 12; hour = hour ? hour : 12; // Handle midnight (0 hours) document.getElementById(‘time’).innerHTML = `${hour}:${minute}:${second}:${ampm}`; document.getElementById(‘date’).textContent = `${day}-${month}-${year}`; } // Update time every second setInterval(updateTime, 1000); // Initial call to display time immediately updateTime();

Guide Video

Here’s how we strategically created the digital clock

We started by defining the objectives of the digital clock, such as displaying accurate time and date information in a stylish and functional manner. We researched existing digital clock implementations and planned the features and design elements we wanted to incorporate. Based on our objectives and research, we selected JavaScript for real-time updates, HTML for structure, and CSS for styling.

We wrote JavaScript functions to format the current time and date according to our desired display format. Utilizing JavaScript’s setInterval function, we ensured that the clock updates every second to display the current time accurately. We implemented logic to support both 12-hour and 24-hour time formats, catering to different preferences. For the 12-hour format, we added functionality to display the appropriate AM/PM indicator. We included code to retrieve and format the current date, displaying it alongside the time. Using CSS, we styled the clock to match the desired aesthetic, including font styles, sizes, colors, and layout.

Wrapping Up

Elevate your website’s allure with our meticulously crafted digital clock code. Seamlessly integrating style and functionality, it’s the perfect solution to infuse sophistication into your web presence. Try it now and embrace a new era of timekeeping excellence!

Leave a Reply

Your email address will not be published. Required fields are marked *