<h2 id="clock">00:00:00</h2>
const clock = document.querySelector("h2#clock");

function getClock() {
  const date = new Date();
  
  //String: String으로 감싸서 문자로 변환하기
  //padStart: 문자열이 2자 미만일 경우 "0"으로 채워서 해당 길이만큼 만들기
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  const seconds = String(date.getSeconds()).padStart(2, "0");
  
  //``백틱 사용
  clock.innerText = `${hours}:${minutes}:${seconds}`
}

//getClock(); //계속 호출
setInterval(getClock, 1000);

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.