Я сделал это, ожидайте, что это было о вашем вопросе?
function currentTime(elementsClass)
{
let AllClock = document.getElementsByClassName(elementsClass)
, theClock = AllClock[0]
, hours = document.createElement('span')
, points = document.createElement('span')
, mins = document.createElement('span')
, blank = document.createElement('span')
, middays = document.createElement('span')
, date = null
, hour = null
, theClockInnerHTML = ''
;
theClock.appendChild(hours);
theClock.appendChild(points);
theClock.appendChild(mins);
theClock.appendChild(blank);
theClock.appendChild(middays);
points.textContent = ":"
blank.textContent = " "
updateClock()
setInterval(updateClock, 1000);
function updateClock()
{
date = new Date()
hour = date.getHours()
hours.textContent = time2digits( (hour == 0) ? 12 : ((hour > 12) ? (hour - 12): hour) )
mins.textContent = time2digits( date.getMinutes())
middays.textContent = (hour >= 12) ? "PM" : "AM"
if (theClock.innerHTML!=theClockInnerHTML) {
theClockInnerHTML = theClock.innerHTML
for(let i=1;i<AllClock.length;i++) {
AllClock[i].innerHTML = theClockInnerHTML
}
}
}
function time2digits(k)
{
return (k < 10) ? ("0" + k) : k
}
}
currentTime('clock');
<p class="clock"></p>
<p class="clock"></p>
<p class="clock"></p>