์ด๋ฆฐ์ด๋ ์ ๊ธฐ์ค์ผ๋ก ์นด์ดํธ๋ค์ด ์นํ์ด์ง๋ฅผ ๋ง๋ค์ด๋ดค์ด์!
const clockTitle = document.querySelector(".js-clock");
function getClock() {
const dday = new Date("December 25, 22 00:00:00").getTime();
const today = new Date().getTime();
const gap = dday - today;
console.log(gap);
const day = Math.floor(gap / (1000 * 60 * 60 * 24));
const hour = String(Math.floor((gap / (1000 * 60 * 60)) % 24)).padStart(
2,
"0");
const min = String(Math.floor((gap / (1000 * 60)) % 60)).padStart(2, "0");
const sec = String(Math.floor((gap / 1000) % 60)).padStart(2, "0");
clockTitle.innerText =
day + "d " + `${hour}` + "h " + `${min}` + "m " + `${sec}` + "s ";
}
getClock();
setInterval(getClock, 1000);
https://codesandbox.io/embed/a08blueprint-forked-i3dzq3?fontsize=14&hidenavigation=1&theme=dark
โ๏ธ๊ถ๊ธํ์ โ๏ธ
๋๋ hour, min, sec ์ผ๋ก ํ์๋๋ฐ hour2, min2, sec2๋ก ํผ ๋ถ์ ๊ธ์ ๋ดค๋ค.
์ถ๋ ฅ๋๋ ๊ฐ์ ๋ชจ๋ ๊ฐ๊ฒ ๋์๋๋ฐ, ์ด๋ค ์ ์ด ๋ค๋ฅธ์ง ๋ชจ๋ฅด๊ฒ ๋ค...
๊ณ์ ๊ณต๋ถํ๋ฉด์ ์๊ฒ ๋๋ฉด ๋ธ๋ก๊ทธ์ ์ ๋ฐ์ดํธ ํ๊ฒ ์ต๋๋ค!
const hour = String(Math.floor(gap / (1000 * 60 * 60) % 24));
const hour2 = String(Math.floor(gap % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)));
const min = String(Math.floor(gap / (1000 * 60) % 60));
const min2= String(Math.floor((gap%(1000 * 60 * 60))/(1000*60)));
const sec = String(Math.floor(gap / 1000 % 60));
const sec2= String(Math.floor((gap%(1000*60))/1000));
ํน์ ์์๋ ๋ถ ๊ณ์๋ฉด ์ง๋๊ฐ๋ค๊ฐ ๋๊ธ ๋ถํํฉ๋๋ค.