I am trying to build a digital clock with HTML, CSS and JavaScript. I made a buttun that will toggle between 12hr and 24hr system. I am stuck with the logic on how to make that possible.
function displayTime(){
let d = new Date()
let hour = d.getHours()
let minute = d.getMinutes()
let second = d.getSeconds()
let amOrPm = "AM"
let clockSystem = document.getElementById("24hr-btn")
// Indicates whether its AM or PM
if (hour >= 12){
amOrPm = "PM"
}
// Toggle Clock System
function clockSystemTime(){
// sets the clock to the 12hr system clock
if (hour > 12) {
hour -= 12
} else {
hour += 12
}
}
if (hour < 0){
hour = "0" + hour
}
if (minute < 10){
minute = "0" + minute
}
if (second < 10){
second = "0" + second
}
document.getElementById("clock").innerHTML = hour + ":" + minute + ":" + second + " " + amOrPm;
}
setInterval(displayTime, 1000)
your text