Created
February 9, 2022 04:09
-
-
Save w3collective/010360d91905959846eba2d60b27a1b9 to your computer and use it in GitHub Desktop.
Digital clock using JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source => https://w3collective.com/digital-clock-javascript/