DEV Community

Rutvik Patel
Rutvik Patel

Posted on • Updated on • Originally published at rutikkpatel.Medium

How to Create a Digital Clock Using JavaScript

Digital Clock Using JavaScript

In this article, we’ll show you “How to create a digital clock using JavaScript” In this article, I will guide you through the process of building a simple digital clock that can display the current time on your webpage.

We will start by creating a basic HTML document and adding some CSS to style the clock. Then, we will use JavaScript to get the current time and update the clock every second.

Throughout the article, I will use the key concepts and functions of JavaScript, such as the Date object and setInterval() method. By the end of this blog, you will have a fully functional digital clock that you can use on your own website.

Whether you are a beginner or an experienced developer, this article is perfect for anyone who wants to learn more about JavaScript and create an interactive element for their website. So, grab your favorite code editor, and let’s get started!

Created By [Author]( ( [Rutik Patel]( )


Points to be discussed

  • Preview

  • YouTube Tutorial

  • HTML Code

  • CSS Code

  • JavaScript Code

  • References


Preview :

A live demo of the website can be viewed by clicking here.

Preview of Digital Clock


YouTube Tutorial :




<!DOCTYPE html>
<html lang="en">

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>JavaScript Digital Clock</title>

  <link rel="stylesheet" href="style.css">
  <!-- Bootstrap CDN -->
  <link href="" rel="stylesheet"
    integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
  <!-- My JavaScript -->
  <script src="script.js"></script>

  <div class="container my-5 py-4 bg-warning">
    <div class="jumbotron">
      <h1 class="display-4">Current Time : <span id="time"></span></h1>
      <hr size=5px;>
      <h1 class="display-5">Current Date : <span id="date"></span></h1>

  <div class="text-center">
    <h1>DIGITAL CLOCK USING JavaScript</h1>

    <p id="footer" class= "text-center">
      © 2023 Designed By : <a href="" target="_block">Rutik Patel</a>
  <!-- Bootstrap Bundle Script CDN -->
  <script src=""

Enter fullscreen mode Exit fullscreen mode




* {
  margin: 0;
  padding: 0;

body {
  background: rgba(162, 155, 254, 0.5) !important;

.container {
  border-radius: 15px;

#footer a {
  text-decoration: none;
  line-height: 34px;
  color: #000000;


#footer a:hover {
  color: red;
  font-weight: bold;
  text-decoration: underline;
Enter fullscreen mode Exit fullscreen mode


JavaScript CODE :


let a;
let time;
let date;
const options = {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric'

setInterval(() => {
  a = new Date();
  // Concate Hours, minutes and seconds
  time = a.getHours() + ":" + a.getMinutes() + ":" + a.getSeconds();
  document.getElementById('time').innerText = time;

  // For Current Date
  // 14-02-2023 Date Format
  // date = a.toLocaleDateString();

  // Display month day in string format - Wednesday, 20 December 2000
  date = a.toLocaleDateString(undefined, options)
  document.getElementById('date').innerText = date;
Enter fullscreen mode Exit fullscreen mode


References :

GitHub Repository:

Top comments (2)

gottz profile image
Jan-Stefan Janetzky

setInterval without interval? so you exec this a thousand times a second? why...

you can just calc the remaining time until the next second and use that in a recursive setTimeout instead...

rutikkpatel profile image
Rutvik Patel

Thank you for your suggestion.