Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to make Digital Clock JavaScript

Posted on Sep 5 Welcome to yet another exciting tutorial by stakedesigner. Building Digital Clock JavaScript projects is one of the best ways to learn JavaScript. So today let’s build a Digital Clock JavaScript’. To build this project we need HTML, CSS, Javascript.Here Source Code link In today’s digital age, knowing how to create a digital clock using JavaScript is a valuable skill for web developers. Whether you want to display the current time on your website or build a custom clock application, JavaScript provides the necessary tools to make it happen. In this tutorial, we’ll walk you through the process of creating a simple digital clock using HTML, CSS, and JavaScript.Before we dive into the code, make sure you have the following:Basic knowledge of HTML, CSS, and JavaScript.A code editor (e.g., Visual Studio Code, Sublime Text).A web browser to test your digital clock.Let us explore the project folder structure. The project folder consists of 3 files. The HTML file creates the elements required to build the structure and layout of our project. Next, the CSS file styles the elements that we have created with CSS and Finally, Javascript adds functionality to our project.We begin with the HTML code. Copy the code below and paste it into your HTML document. Here, we create a div with the class ‘clock’ and inside this, we create divs for displaying hours, minutes, and seconds. Apart from this we also use span tags to display ‘:’ between the divs.

Digital Clock
:
:
Next, we style our game using CSS. For this copy, the code provided to you below and paste it into your stylesheet.We provide a background color to the body, while the time is displayed on a white background. Each element is aligned correctly and given a bit of a curve to make it look good.* { padding: 0; margin: 0; box-sizing: border-box; font-family: "Poppins", sans-serif;}body { height: 100vh; background-image: linear-gradient(135deg, #8052ec, #d161ff);}.clock { width: 31.25em; height: 8em; position: absolute; transform: translate(-50%, -50%); top: 50%; left: 50%; display: flex; align-items: center; justify-content: space-between; font-weight: 600;}.clock div { position: relative; background-color: #ffffff; height: 100%; width: 3.2em; display: flex; align-items: center; justify-content: center; font-size: 2.5em; color: #150c41; border-radius: 0.4em; box-shadow: 0 1em 2em rgba(0, 0, 0, 0.3); letter-spacing: 0.05em;}.clock span { font-weight: 800; font-size: 2.5em; color: #ffffff;}Finally, we add functionality using Javascript. Once again copy the code below and paste it into your script file.We create a ‘clock’ variable in which we call the function ‘time’, this function updates the time every second by getting the current time using the JavaScript Date() object. The hours, minutes, and seconds are then extracted from the date and formatted with leading zeros using the padStart() function. Next, formatted time is then displayed on the webpage as “hour”, “minute”, and “seconds”.`const hour = document.getElementById("hour");const minute = document.getElementById("minute");const seconds = document.getElementById("seconds");const clock = setInterval(function time() { const dateNow = new Date(); let hr = dateNow.getHours(); let min = dateNow.getMinutes(); let sec = dateNow.getSeconds();hr = hr.toString().padStart(2, "0"); min = min.toString().padStart(2, "0"); sec = sec.toString().padStart(2, "0");const timeString = ${hr}:${min}:${sec}; hour.textContent = hr; minute.textContent = min; seconds.textContent = sec;}, 1000);`Templates let you quickly answer FAQs or store snippets for re-use.You could cut the JS just a few lines: Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse NFT Research - Sep 5 Nudle - Sep 5 amber Yao - Sep 5 rabbit - Sep 5 Once suspended, stakedesigner will not be able to comment or publish posts until their suspension is removed. Once unsuspended, stakedesigner will be able to comment and publish posts again. Once unpublished, all posts by stakedesigner will become hidden and only accessible to themselves. If stakedesigner is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to StakeDesigner. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag stakedesigner: stakedesigner consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging stakedesigner will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.


This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

How to make Digital Clock JavaScript

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×