Creating a clock using JavaScript

Imagine a clock that shows the time to your users, on a travel site for example, with the time zone of the destination country? Let's create this magnificent clock together, using JavaScript!
•
Updated on August 14, 2026
•
2 min read
Believemy logo

Imagine a clock that shows the time to your users, for example on a travel website with the destination country's time zone? Let’s create this magnificent clock together easily with JavaScript!

Image

 

Step 1

Let's start with the HTML code.

HTML
<div class="clock">
  <div class="wrap">
    <span class="heure"></span>
    <span class="minute"></span>
    <span class="seconde"></span>
    <span class="dot"></span>
  </div>
</div>

.clock defines the clock, .wrap defines the background of the clock, .heure, .minute, .seconde are the hands, and .dot represents the central circle that unites the hands.

 

Step 2

Let's style the clock and its background.

CSS
.clock {
	 border-radius: 50%;
	 background: #fff;
	 border: 5px solid white;
	 box-shadow: inset 2px 3px 8px 0 rgba(0, 0, 0, 0.1);
}

.wrap {
	 position: relative;
	 width: 350px;
	 height: 350px;
}

 

Step 3

Let's move on to the hands.

CSS
.minute, .heure {
	 position: absolute;
	 height: 100px; width: 6px;
	 margin: auto;
	 top: -27%; left: 0;	bottom: 0; right: 0;
	 transform-origin: bottom center;
	 transform: rotate(0deg);
	 box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.4);
     background: black;
}

 

Step 4

Let's refine the minute hand.

CSS
.minute {
	 position: absolute;
	 height: 130px;
	 width: 4px;
	 top: -38%;
	 left: 0;
	 box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.4);
	 transform: rotate(90deg);
}

After styling the minute and hour hands, we will now style the minute hand more precisely to differentiate them.

 

Step 5

Let's move on to the second hand.

CSS
.seconde {
	 position: absolute;
	 height: 90px;
	 width: 2px;
	 margin: auto;
	 top: -26%;
	 left: 0;
	 bottom: 0;
	 right: 0;
	 border-radius: 4px;
	 background: #ff4b3e;
	 transform-origin: bottom center;
	 transform: rotate(180deg);
}

We will style and make the second hand red.

 

Step 6

Let's handle the central circle.

CSS
.dot {
	 position: absolute;
	 top: 0;
	 left: 0;
	 right: 0;
	 bottom: 0;
	 width: 12px;
	 height: 12px;
	 border-radius: 50%;
	 background: white;
	 border: 2px solid #1b1b1b;
	 border-radius: 50%;
	 margin: auto;
}

This central circle will serve to unite all the hands at a single point.

 

Step 7

Let's finish with JavaScript to make our clock functional.

JAVASCRIPT
clock();

function clock() {
  const date = new Date();
  const hours = ((date.getHours() + 11) % 12 + 1);
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();
  const hour = hours * 30;
  const minute = minutes * 6;
  const second = seconds * 6;
  
  document.querySelector('.heure').style.transform = `rotate(${hour}deg)`;

  document.querySelector('.minute').style.transform = `rotate(${minute}deg)`;

  document.querySelector('.seconde').style.transform = `rotate(${second}deg)`;
}

setInterval(clock, 1000);
Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Make an animated notification message with JavaScript

Whether it's to quickly warn your users that a feature of your site is not yet available or to let them know that an action has been successfully completed, notifications are the simplest and most modern way to set up. Find out how in this article!

December 9, 2024

Create an image slideshow

Let's discover together how to create a simple and effective image slideshow using HTML, CSS and JavaScript!

December 6, 2024

Create a map displaying links on mouse-over with CSS

To present your team or simply yourself, this animation will allow you to display the links to your social networks when you hover over the map: a striking effect guaranteed! Let's find out how to do it together.

December 9, 2024

Create a clean, modern timeline using html and css

Whether you want to show a purchasing process, the highlights of your career on your portfolio, or anything else, learn how to create a sleek, modern timeline to show it off!

December 9, 2024

Creating an animated list with CSS

Let's find out how to create an animated hover list!

December 9, 2024

Create hover text on image with CSS

We've already seen how to create an element that is displayed when the mouse hovers over an image, but in this article we'll show you how to display a whole host of information very fluidly by hovering the mouse over an element!

December 9, 2024

Create a menu that spans the entire page using CSS

From time to time we have to ask our visitors which page they want to go to, by displaying the main sections of our website. This menu is often not very pleasant to use, so let's find out together in this article how to make it much more modern, with beautiful animations that will make your passers-by want to visit your site!

December 9, 2024

Everything you need to know about the Temporal API with JavaScript

Discover JavaScript's Temporal API: classes, practical examples, time zone handling, and migration from Date. The ultimate guide to mastering Temporal.

March 20, 2026

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.