Programming/Javascript
자바스크립트 d-day timer 디데이 타이머 만들기
류시명
2020. 4. 24. 08:12
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();
반응형