Skip to content

Instantly share code, notes, and snippets.

@w3collective
Created February 9, 2022 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w3collective/010360d91905959846eba2d60b27a1b9 to your computer and use it in GitHub Desktop.
Save w3collective/010360d91905959846eba2d60b27a1b9 to your computer and use it in GitHub Desktop.
Digital clock using JavaScript
const clock = document.createElement("div");
document.body.appendChild(clock);
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;
let meridiem;
if (hour > 12) {
meridiem = "PM";
hour = hour - 12;
} else {
meridiem = "AM";
}
const time = hour + ":" + min + ":" + sec + " " + meridiem;
clock.innerText = time;
};
setInterval(getTime, 1000);
@w3collective
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment