I've been searching for a solution to this problem without much success. Essentially, I have a clock displayed in a 12-hour format that automatically appears when the page loads. My goal is to switch the clock format to 24 hours using an AddEventListener. Additionally, I would also like to implement another AddEventListener to remove the seconds display. Below is the code snippet I'm currently working with:
function myClock() {
let date = new Date();
let hh = date.getHours();
let mm = date.getMinutes();
let ss = date.getSeconds();
let session = "AM";
if(hh == 0){
hh = 12;
}
if(hh > 12){
hh = hh - 12;
session = "PM";
}
hh = (hh < 10) ? "0" + hh : hh;
mm = (mm < 10) ? "0" + mm : mm;
ss = (ss < 10) ? "0" + ss : ss;
let time = hh + ":" + mm + ":" + ss + " " + session;
document.getElementById("time-now").innerText = time;
var t = setTimeout(function(){myClock()}, 1000);
}
myClock();
<span id="time-now"></span>