티스토리 뷰

const body = document.querySelector("body");
const timer = document.createElement("h2");
const title = document.createElement("h1");
body.prepend(timer);
body.prepend(title);

function getTime() {
  const target = new Date("2020-12-24 00:00:00+0900");
  const today = new Date();
  const gap = target - today;
  const d = Math.floor(gap / (1000 * 60 * 60 * 24)); // 일
  const h = Math.floor((gap / (1000 * 60 * 60)) % 24); // 시
  const m = Math.floor(((gap / 1000) * 60) % 60); // 분
  const s = Math.floor((gap / 1000) % 60); // 초
  if (gap <= 0) {
    title.innerText = "당일입니다.";
    timer.innerText = "";
  } else {
    title.innerText = "그날까지";
    timer.innerText = `${d}일 ${h}시간 ${m}분 ${s}초 남았습니다.`;
  }
}

function init() {
  getTime();
  setInterval(getTime, 1000);
}
init();
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함