Very Simple Javascript Clock (Free Download)

Welcome to a tutorial on how to create a simple Javascript clock. Need to add a clock to your website? Or just learning about timers in Javascript? Well, here’s a really simple example – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

 

DOWNLOAD & DEMO

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

JAVASCRIPT CLOCK DEMO

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

JAVASCRIPT CLOCK

All right, let us now get into more details on how the Javascript clock works.

 

PART 1) THE HTML

js-clock.html
<div id="clockWrap"></div>

Yep, that’s all. A good old <div id="clockWrap"> container to show the clock.

 

PART 2) THE JAVASCRIPT

js-clock.js
window.onload = () => {
  // (A) GET HTML <DIV ID="CLOCKWRAP">
  var hClock = document.getElementById("clockWrap");
 
  // (B) HELPER FUNCTION TO PREPEND 0
  var pzero = num => num<10 ? "0" + num : num ;
 
  // (C) START TIMER
  var timer = setInterval(() => {
    let now = new Date();
    hClock.innerHTML = `${pzero(now.getHours())} :
    ${pzero(now.getMinutes())} :
    ${pzero(now.getSeconds())}`;
  }, 1000);
};
  1. Self-explanatory, get <div id="clockWrap">.
  2. pzero(num) is a helper function to prepend a 0 to num if it is less than 10.
  3. var timer is set to run every second.
    • let now = new Date(); Get the current date/time.
    • hClock.innerHTML = ... Reformat the current time, and set it into <div id="clockWrap">.

Read up on Javascript ternary operator and arrow functions if you are lost. Arrow functions are the “shorthand way to define functions”, and ternary is the “shorthand way to define if-else”. Basically:

var pzero = function (num) {
  if (num < 10) { return "0" + num; }
  else { return num; }
}; 

 

 

PART 3) THE CSS

js-clock.css
#clockWrap {
  /* (A) FONT */
  font-family: Arial, sans-serif;
  font-size: 40px;

  /* (B) CENTER */
  text-align: center;
}

I don’t think this even needs an explanation… Literally just setting the font, font size, and centering the text.

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

EXTRA) HOW ABOUT ADDING THE DATE?

It should not be too difficult once you have figured out the majority of the above. I shall leave this as “homework” with some hints:

  • now.getFullYear()
  • now.getMonth()
  • now.getDate()
  • now.getDay()

 

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.