๐Ÿ“ŒLanguage/JavaScript

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๋งŒ๋“œ๋Š” ๋””๋ฐ์ด ์นด์šดํŠธ๋‹ค์šด ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ

hellohailie 2022. 4. 20. 18:55
๋ฐ˜์‘ํ˜•

์–ด๋ฆฐ์ด๋‚ ์„ ๊ธฐ์ค€์œผ๋กœ ์นด์šดํŠธ๋‹ค์šด ์›นํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ดค์–ด์š”!

 

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 

 

a08Blueprint (forked) - CodeSandbox

a08Blueprint (forked)

codesandbox.io

 

 


 

โ—๏ธ๊ถ๊ธˆํ•œ์ โ—๏ธ

 

๋‚˜๋Š” 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));

 

 

ํ˜น์‹œ ์•„์‹œ๋Š” ๋ถ„ ๊ณ„์‹œ๋ฉด ์ง€๋‚˜๊ฐ€๋‹ค๊ฐ€ ๋Œ“๊ธ€ ๋ถ€ํƒํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•