How to make a digital clock using JavaScript

In this tutorial we’ll be building a digital clock using JavaScript.

Let’g get started by creating a <div> element that’ll be used to display the time:

const clock = document.createElement(“div”);
document.body.appendChild(clock);

Next …

In this tutorial we’ll be building a digital clock using JavaScript.

Let’g get started by creating a <div> element that’ll be used to display the time:

const clock = document.createElement("div");
document.body.appendChild(clock);

Next create a getTime function that’ll calculate the time:

const getTime = () => {
  const date = new Date();
  let hour = date.getHours();
  let min = date.getMinutes();
  let sec = date.getSeconds();
}

We’ve used JavaScripts built in Date() object here which represents a single moment in time. From this we then extract the hours, minutes, and seconds. Each measurement is assigned it’s own variable as we’ll need to do some manipulation in the next step.

Hours, minutes, and seconds less than 10 don’t have a leading zero digit which is commonly used when formatting digital time. To achieve this we’ll check if the unit of measure is less than 10. If it is we’ll prepend a zero otherwise the measurement is left alone:

const getTime = () => {
  const date = new Date();
  let hour = date.getHours();
  let min = date.getMinutes();
  let sec = date.getSeconds();
  hour = (hour < 10) ? "0" + hour : hour;
  min = (min < 10) ? "0" + min : min;
  sec = (sec < 10) ? "0" + sec : sec;   
};

We can now insert the time to our clock <div>:

const getTime = () => {
  const date = new Date();
  let hour = date.getHours();
  let min = date.getMinutes();
  let sec = date.getSeconds();  
  hour = ("0" + hour).slice(-2);
  min = ("0" + min).slice(-2);
  sec = ("0" + sec).slice(-2); 
  const time = hour + ":" + min + ":" + sec;
  clock.innerText = time;
};

If you were to run the getTime function at this point it would fetch the current time at execution. As we’re building a digital clock we’ll need to fetch and update the time using the SetInterval method every second (1000 milliseconds):

const getTime = () => {
  const date = new Date();
  let hour = date.getHours();
  let min = date.getMinutes();
  let sec = date.getSeconds();  
  hour = ("0" + hour).slice(-2);
  min = ("0" + min).slice(-2);
  sec = ("0" + sec).slice(-2); 
  const time = hour + ":" + min + ":" + sec;
  clock.innerText = time;
};
setInterval(getTime, 1000);

Currently the clock is displaying time in the 24 hour format. To convert to a 12 hour format clock we just need to check if the current hour is greater than 12, if true subtract 12 hours. We’ll also declare a merdiem variable and assign it either a AM or PM which will get displayed after the time:

const getTime = () => {
  const date = new Date();
  let hour = date.getHours();
  let min = date.getMinutes();
  let sec = date.getSeconds();
  hour = ("0" + hour).slice(-2);
  min = ("0" + min).slice(-2);
  sec = ("0" + sec).slice(-2);
  let meridiem;
  if (hour > 12) {
    meridiem = "PM";
    hour = hour - 12;
  } else {
    meridiem = "AM";
  }
  const time = hour + ":" + min + ":" + sec + " " + meridiem;
  clock.innerText = time;
};

You should now have a fully functioning digital clock built using JavaScript. When it comes to styling this clock it would be best to use a monospace font (font-family: monospace;). This will prevent the clock dimensions shifting due to the variable font width between the different numbers.


Print Share Comment Cite Upload Translate
APA
Michael Burrows | Sciencx (2024-03-29T14:13:44+00:00) » How to make a digital clock using JavaScript. Retrieved from https://www.scien.cx/2023/03/24/how-to-make-a-digital-clock-using-javascript/.
MLA
" » How to make a digital clock using JavaScript." Michael Burrows | Sciencx - Friday March 24, 2023, https://www.scien.cx/2023/03/24/how-to-make-a-digital-clock-using-javascript/
HARVARD
Michael Burrows | Sciencx Friday March 24, 2023 » How to make a digital clock using JavaScript., viewed 2024-03-29T14:13:44+00:00,<https://www.scien.cx/2023/03/24/how-to-make-a-digital-clock-using-javascript/>
VANCOUVER
Michael Burrows | Sciencx - » How to make a digital clock using JavaScript. [Internet]. [Accessed 2024-03-29T14:13:44+00:00]. Available from: https://www.scien.cx/2023/03/24/how-to-make-a-digital-clock-using-javascript/
CHICAGO
" » How to make a digital clock using JavaScript." Michael Burrows | Sciencx - Accessed 2024-03-29T14:13:44+00:00. https://www.scien.cx/2023/03/24/how-to-make-a-digital-clock-using-javascript/
IEEE
" » How to make a digital clock using JavaScript." Michael Burrows | Sciencx [Online]. Available: https://www.scien.cx/2023/03/24/how-to-make-a-digital-clock-using-javascript/. [Accessed: 2024-03-29T14:13:44+00:00]
rf:citation
» How to make a digital clock using JavaScript | Michael Burrows | Sciencx | https://www.scien.cx/2023/03/24/how-to-make-a-digital-clock-using-javascript/ | 2024-03-29T14:13:44+00:00
https://github.com/addpipe/simple-recorderjs-demo