Lädt...


🔧 Create a Simple Digital Clock Using HTML, CSS and JavaScript


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

If you already possess the basic HTML, CSS and JavaScript skills, you know it's time to start building projects. It would be wise to get started building a few easy ones then moving up the ladder step by step. One such beginner-friendly projects would be a 'Digital Clock'

In this article, we'll learn to build a simple Digital Clock using some of the basic web development skills that you currently possess. We'll not work much on the UI of the clock, but in fact focus a lot on how to use JavaScript to put everything together to build the clock. so, let's get started.

First let's work on the markup. All we need is a single div element with a class of clock.

<div class="clock">16:20:35</div>

This div element will contain the digits of the clock in the hh:mm:ss format. For now, I've simply added some placeholder digits, which will later be replaced dynamically with the current time using JavaScript. Now, let's add a few CSS styles.

@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;600&display=swap');

body{
  background: #000;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.clock{
  color: #fff;
  font-family: 'Orbitron', sans-serif;
  font-size: 6rem;
  letter-spacing: 1rem;
  font-weight: bold;
}

In the above styles, notice that we are using a custom Google font named Orbitron, which is best-suited for a digital clock. I've also added a few flex properties to the body simply to
vertically and horizontally center the clock. Lastly, a few styles have been added to the .clock div to slightly increase the font size and add a bit of a spacing. Below is a screen shot of what the clock looks like.

Image of a digital clock

Lastly, let's start working on the most important part, the JavaScript. First let's create a variable named clock. This variable will store the div element that has a class of clock.

let clock = document.querySelector('.clock')

Now, we need to create a function to get the current hours, minutes and seconds. We'll name the function as updateTime.

function updateTime(){
  let hours = new Date().getHours();
  let minutes = new Date().getMinutes();
  let seconds = new Date().getSeconds(); 
}

In the above function, the new Date() constructor gets the current date and time. The 3 methods getHours(), getMinutes(), getSeconeds() give us the current hour, minutes and seconds respectively. If we run this function in the console, we get:

screenshot of the function updateTime
In the above screenshot, you can see that the function returns hours, minutes and seconds as 12, 58 and 35. That's because the current time on my system is 12:58:35.

The next thing we need to do is simply insert the hours, minutes and the seconds in the div element which has the class of clock. For this, we'll be using the innerText property. Add the below line inside the function updateTime().

clock.innerHTML = `${hours}:${minutes}:${seconds}`;

One additional thing we need to do is add an extra '0' if the digits are below 10. For instance if the time is 1:15:5, we want it to display 01:15:05. Add the below 3 lines of code in the function updateTime() just before the line clock.innerText = .....

hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;

In the first line of code above, all we are trying to say is 'if the hours is less than 10, which means, if it is anything between 0 to 9, add an extra 0 in front of it. Or else, keep it as it is'. We are doing the same thing with the minutes and the seconds.

So far, we have the below JavaScript code:

let clock = document.querySelector('.clock')

function updateTime(){
  let hours = new Date().getHours();
  let minutes = new Date().getMinutes();
  let seconds = new Date().getSeconds();

  hours = hours < 10 ? "0" + hours : hours;
  minutes = minutes < 10 ? "0" + minutes : minutes;
  seconds = seconds < 10 ? "0" + seconds : seconds;

 clock.innerHTML = `${hours}:${minutes}:${seconds}`;
}

Now, all we need to do is simply display the time on our screen by calling the function updateTime() at the very end, outside the function block.

let clock = document.querySelector('.clock')

function updateTime(){
  let hours = new Date().getHours();
  let minutes = new Date().getMinutes();
  let seconds = new Date().getSeconds();

  hours = hours < 10 ? "0" + hours : hours;
  minutes = minutes < 10 ? "0" + minutes : minutes;
  seconds = seconds < 10 ? "0" + seconds : seconds;

 clock.innerHTML = `${hours}:${minutes}:${seconds}`;
}
updateTime();

Here's what our clock looks like:

Image of a digital clock

But, our clock doesn't dynamically update yet. You need to refresh every time you want to check the updated time. So, to dynamically update our clock every second, we will be using the setTimeout() method. Add the below line inside the function block at the very end.

setTimeout(updateTime, 1000);

This setTimeout() method will call the function updateTime every 1000 milliseconds, i.e. after every second. So, the final JavaScript code that we have is:

let clock = document.querySelector('.clock')

function updateTime(){
  let hours = new Date().getHours();
  let minutes = new Date().getMinutes();
  let seconds = new Date().getSeconds();

  hours = hours < 10 ? "0" + hours : hours;
  minutes = minutes < 10 ? "0" + minutes : minutes;
  seconds = seconds < 10 ? "0" + seconds : seconds;

 clock.innerHTML = `${hours}:${minutes}:${seconds}`;

setTimeout(updateTime, 1000);
}
updateTime();

If you've done everything right, this is how your digital clock will look like:

A .gif image displaying a digital clock

In the above .gif image, you can notice that the clock is slightly shifting left and right, and that is because the digits do not have an even width. One of the easiest way to eliminate this issue is to remove the justify-content property given to the body element and give a margin-left to the clock div. I found 'margin-left:30%' seemed good enough to center the clock on larger displays.

So, that was a simple tutorial on how to create a digital clock using HTML, CSS and JavaScript. Hope you like it. Have a beautiful day.

...

🔧 Create a Simple Digital Clock Using HTML, CSS and JavaScript


📈 59.35 Punkte
🔧 Programmierung

🔧 Create a Futuristic 3D Digital Clock with HTML, CSS, and JavaScript


📈 47.29 Punkte
🔧 Programmierung

🔧 How to make a clock using html , JavaScript and CSS and deploy it using firebase


📈 45.62 Punkte
🔧 Programmierung

🔧 How To Create a Calculator Using HTML CSS & JavaScript | Simple Calculator in JavaScript


📈 44.45 Punkte
🔧 Programmierung

🔧 Introduction of CSS, What is CSS, Why we use CSS and How CSS describe the HTML elements


📈 38.69 Punkte
🔧 Programmierung

🔧 Create Digital Clock Using JavaScript


📈 37.58 Punkte
🔧 Programmierung

🔧 How to Create a Digital Clock Using JavaScript


📈 37.58 Punkte
🔧 Programmierung

🔧 How To Create A Simple Audio-player in HTML, JavaScript, and CSS


📈 35.04 Punkte
🔧 Programmierung

🔧 How to create a simple mobile responsive navigation bar with HTML, CSS and JavaScript


📈 35.04 Punkte
🔧 Programmierung

🔧 How to Create a Simple Tab Navigation UI with HTML, CSS, and JavaScript


📈 35.04 Punkte
🔧 Programmierung

🔧 Build a Tic Tac Toe Game using HTML, CSS, JavaScript, Tailwind CSS and Canvas Confetti


📈 33.48 Punkte
🔧 Programmierung

🔧 Create a Simple Website with HTML, CSS, JavaScript


📈 33.43 Punkte
🔧 Programmierung

🔧 Design a simple counter using HTML CSS and JavaScript


📈 32.79 Punkte
🔧 Programmierung

🔧 Building a Simple Snake Game Using HTML, CSS, and JavaScript


📈 32.79 Punkte
🔧 Programmierung

🔧 How to create navigation menu with HTML CSS step by step | web design tutorial | HTML CSS tutorial


📈 32.33 Punkte
🔧 Programmierung

🔧 Create Two RGB cursor following lines using html, CSS and JavaScript


📈 32.24 Punkte
🔧 Programmierung

🔧 How to Create a Responsive Lightbox Gallery with Thumbnails using HTML, CSS, and JavaScript


📈 32.24 Punkte
🔧 Programmierung

🔧 Create a Pomodoro Timer App using HTML, CSS, and JavaScript


📈 32.24 Punkte
🔧 Programmierung

🔧 How to Create a Loading Bar Using HTML, CSS, and JavaScript


📈 32.24 Punkte
🔧 Programmierung

🔧 How to Create A Color Picker Tool Using HTML, CSS, and JavaScript


📈 32.24 Punkte
🔧 Programmierung

🐧 Clock: a fullscreen clock with seconds, date, and day of the week for Windows and Linux


📈 31.25 Punkte
🐧 Linux Tipps

🔧 Digital Clock Using JavaScript


📈 30.7 Punkte
🔧 Programmierung

🔧 How to make a digital clock using JavaScript


📈 30.7 Punkte
🔧 Programmierung

🔧 How to Create Personal Blog Website Using HTML CSS JavaScript


📈 30.63 Punkte
🔧 Programmierung

🔧 Create your own 📷 image compressor & to webp convertor using HTML, CSS, JavaScript from scratch 🚀


📈 30.63 Punkte
🔧 Programmierung

🔧 Cursor Trail using html css js #virals #js #html #css #work


📈 30.08 Punkte
🔧 Programmierung

🍏 World Clock Deluxe 4.19.1 - World time clock and meeting planner.


📈 29.64 Punkte
🍏 iOS / Mac OS

🔧 How to create your own Paint and use it using javascript and CSS (Houdini API)?


📈 29.24 Punkte
🔧 Programmierung

🔧 Title: Create a Stunning Sliding Login and Registration Form with HTML, CSS, and JavaScript 🚀


📈 29.23 Punkte
🔧 Programmierung

🔧 How to create a swatch clock with Tailwind CSS


📈 29 Punkte
🔧 Programmierung

🐧 How to Create a Simple to-do List With HTML, CSS, and JS


📈 28.64 Punkte
🐧 Linux Tipps

🔧 Build a Simple Bike Website with HTML, CSS, and JavaScript


📈 28.17 Punkte
🔧 Programmierung

🔧 Build a Simple Stopwatch with HTML, CSS, and JavaScript


📈 28.17 Punkte
🔧 Programmierung

🔧 Build a Simple Link Checker Tool with HTML, CSS, and JavaScript


📈 28.17 Punkte
🔧 Programmierung

matomo