How to Create a Digital Clock In HTML, CSS, and JavaScript - Javascript Project

How to Create a Digital Clock In HTML, CSS, and JavaScript - Javascript Project

 

Clocks are a useful element for any UI if used in a proper way. Clocks can be used in sites where time is the main concern like some booking sites or some apps showing arriving times of trains, buses, flights, etc. The clock is basically of two types, Analog and Digital. We will be looking at making a digital one.

Building a digital clock using JavaScript can be an excellent project for a beginner to understand the basic concepts of JavaScript. Creating this will help you know about accessing the DOM, adding activity to them, and much more.

Digital Clock in JavaScript [Source Codes]

To create this program [Digital clock]. First, you need to create three files, HTML File, CSS File, and JavaScript File. After creating these files just paste the following codes into your files. You can also download the source code files of the Digital Clock from the given download buttom.

First, create an HTML file with the name of index.html and paste the given codes in your HTML file. Remember, you’ve to create a file with .html extension.

<!DOCTYPE html>
<html lang="en">
<head>
    <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>Digital Clock in Javascript</title>
    <link rel="stylesheet" href="main.css">
</head>
<body>
    <div class="shape1"></div>
    <div class="shape2"></div>
    <div class="clock">
        <div id="hour">00</div>
        <span>:</span>
        <div id="minute">00</div>
        <span>:</span>
        <div id="seconds">00</div>
        
        <div id="midday">AM</div>
        
    </div>
    <script src="main.js"></script>
</body>
</html>

Second, create a CSS file with the name of style.css and paste the given codes in your CSS file. Remember, you’ve to create a file with .css extension.

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

*,
*::before,
*::after {
    margin0;
    padding0;
    box-sizingborder-box;
}

body {
    height100vh;
    font-family'Orbitron'sans-serif;
    background-color#fbfbfb;
    background-imagelinear-gradient(135deg#131414 0%#000 100%);
}

.clock {
    positionabsolute;
    width750px;
    height150px;
    top50%;
    left50%;
    transformtranslate(-50%-50%);
    displayflex;
    align-itemscenter;
    justify-contentspace-between;
}

.clock div {
    positionrelative;
    width150px;
    height100%;
    background-color#fff;
    box-sizing1px 20px 1px rgb(0 0 0 / 10%);
    displayflex;
    justify-contentcenter;
    align-itemscenter;
    letter-spacing7px;
    font-size60px;
    border-radius5px;
    color#ef4141;
}

.clock span {
    font-size60px;
    font-weightbold;
    color#ef4141;
}

.shape1 {
    positionabsolute;
    width250px;
    height250px;
    backgroundlinear-gradient(45deg#eb9461#ff5b84);
    border-radius50%;
    top15%;
    right15%;
}

.shape2 {
    positionabsolute;
    width150px;
    height150px;
    backgroundlinear-gradient(45deg#1e90ff#fff);
    border-radius50%;
    top45%;
    left15%;
}

Last, create a JavaScript file with the name of script.js and paste the given codes in your JavaScript file. Remember, you’ve to create a file with .js extension.

function showTime() {
  var date = new Date();
  var h = date.getHours();
  var m = date.getMinutes();
  var s = date.getSeconds();
  var session = "AM";

  if ( h == 0) {
    h = 12;
  }

  if ( h > 12) {
    hh -0;
    session = "PM"
  }

  h = (h < 10) ? "0" + h : h;
  m = (m < 10) ? "0" + m : m;
  s = (s < 10) ? "0" + s : s;

  var time_h = h;
  var time_m = m;
  var time_s = s;
  var time_midday = session;
  document.getElementById("hour").innerText = time_h;
  document.getElementById("minute").innerText = time_m;
  document.getElementById("seconds").innerText = time_s;
  document.getElementById("midday").innerText = time_midday;
  setTimeout(showTime1000);
}

// call the function
showTime();


Download Source Code: Create a Digital Clock In HTML, CSS, Javascript.

That’s all, now you’ve successfully Create a Digital Clock In HTML, CSS, Javascript. If your code does not work or you’ve faced any error/problem then please comment down or contact us from the contact page.

Here are a few helpful posts you might want to read, too:

Why learning code? 7 Essential Benefits From Learning Programming

6 Things To Know Before Learning Programming

How to Teach Yourself to Learn Code

If you enjoyed this post about the digital clock in html, csss, javascript just drop me a line in the comments below!

P.S. If you found this post helpful, please share it with others! Thanks for your support!