I am using js to render a 12 hour digital clock that continuously updates. However, the code I found online left me with a 24 hour clock and the wrong am/pm output. Right now it shows 13:33 am. I have tried out other snippets of code here and there, some jquery examples, but nothing worked and a lot of css already went into this one. How would I convert this to a properly functioning 12 hour clock? Thanks!
$(document).ready(function() {
setInterval(function() {
// Create a newDate() object and extract the hours of the current time on the visitor's
var hours = new Date().getHours();
// Add a leading zero to the hours value
$(".hours").html((hours < 10 ? "0" : "") + hours);
}, 1000);
});
setInterval(function() {
// Create a newDate() object and extract the minutes of the current time on the visitor's
var minutes = new Date().getMinutes();
// Add a leading zero to the minutes value
$(".min").html((minutes < 10 ? "0" : "") + minutes);
}, 1000);
setInterval(function() {
var time = new Date().getHours();
var time = (time + 24 - 2) % 24;
var mid = 'am';
if (time == 0) { //At 00 hours we need to show 12 am
hours = 12;
} else if (time > 12) {
time = time % 12;
mid = 'pm';
}
$(".ap").html(mid);
});
ul {
list-style: none;
}
li {
display: inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="clock">
<ul>
<li class="hours"></li>
<li class="point">:</li>
<li class="min"></li>
<li class="ap"></li>
</ul>
</div>