JSFiddle - React, Tailwind, and code Playground
by Paul Wheeler
HTML
<!-- note the use of id="" as opposed to class="" -->
<div id="test"></div>
JavaScript
function startTime() {
const today = new Date();
let h = today.getHours();
let m = today.getMinutes();
let s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('test').innerHTML =
(h > 12 ? h - 12 : (h == 0 ? 12 : h)) + ":" + m + ":" + s + (h >= 12 ? ' p.m.' : ' a.m.');
setTimeout(startTime, 1000);
}
// You need to make an initial call to startTime();
startTime();
function checkTime(i) {
if (i < 10) {
// add zero in front of numbers < 10
i = "0" + i
}
return i;
}