μ½λ 리ν©ν λ§
π μμ μ μ½λ
const merry = document.querySelector(".js-clock");
function getClock() {
const christmas = new Date("2021, 12, 25");
const date = new Date();
const timeGap = christmas - date;
const xDay = Math.floor(timeGap / (1000 * 60 * 60 * 24));
const xHours = Math.floor(
(timeGap - xDay * 1000 * 60 * 60 * 24) / (1000 * 60 * 60)
);
const xMinutes = Math.floor((timeGap % (60 * 60 * 1000)) / (60 * 1000));
const xSeconds = Math.floor((timeGap % (60 * 1000)) / 1000);
const day = String(xDay).padStart(2, "0");
const hours = String(xHours).padStart(2, "0");
const minutes = String(xMinutes).padStart(2, "0");
const seconds = String(xSeconds).padStart(2, "0");
merry.innerText = `${day}d ${hours}h ${minutes}m ${seconds}s`;
}
getClock();
setInterval(getClock, 1000);
π μμ ν μ½λ
const untilChrismas = document.querySelector('.js-clock');
const MSEC_TO_SEC = 1000;
const SEC_TO_MIN = 60;
const MIN_TO_HOUR = 60;
const HOUR_TO_DAY = 24;
paintRemainTime();
setInterval(paintRemainTime, 1000);
function paintRemainTime() {
const { paintingDay, paintingHour, paintingMinute, paintingSecond } = getRemainTime();
untilChrismas.innerHTML = `${paintingDay}d ${paintingHour}h ${paintingMinute}m ${paintingSecond}s`
}
function getRemainTime() {
const christmasDate = new Date('2022, 12, 25');
const nowDate = new Date();
const msecTimeGap = christmasDate - nowDate;
const remainSecond = Math.floor(msecTimeGap / MSEC_TO_SEC);
const remainMinute = Math.floor(remainSecond / SEC_TO_MIN);
const remainHour = Math.floor(remainMinute / MIN_TO_HOUR);
const remainDay = Math.floor(remainHour / HOUR_TO_DAY);
return {
paintingDay: padStartTwoString(remainDay),
paintingHour: padStartTwoString(remainHour % 24),
paintingMinute: padStartTwoString(remainMinute % 60),
paintingSecond: padStartTwoString(remainSecond % 60),
};
}
function padStartTwoString(date) {
return String(date).padStart(2, '0');
}
μμ λ΄μ
date, merry, day, minutes λ± κ·Έλ₯ λ΄€μλ μλ―Έλ₯Ό μ νν μ μ μλ μ΄λ¦λ€μ μμ νμλ€.
1000, 60, 24 κ°μ΄ μμ£Ό μ°μ΄λ μλ€μ μλ―Έλ₯Ό νμ νκ³ μ 리νκΈ° μν΄ μμλ‘ μ μνμλ€.
padStartκ°μ λ°λ³΅ν΄μ μ¬μ©λλ λ©μλλ₯Ό ν¨μλ‘ μ μνμλ€.
μ½λλ₯Ό μ μ΄ν΄ν μ μλλ‘ κ³ μ°¨μμμ μ μ°¨μμΌλ‘ λ‘μ§μ ꡬννμλ€.
μ± μ λ΄μ©μ μ€μ μ½λμ μ μ©ν΄λ³΄λ μ¬λ―Έλ μκ³ νμ€ν μ½λκ° μ 리λλκ²μ νμΈν μ μμλ€.