๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

D-day ๋งŒ๋“ค๊ธฐ

by ํ›„์•ผ- 2024. 1. 31.

๐Ÿ’ก ๊ธ‰ํ•˜๊ฒŒ ํ•˜์ง€ ๋ง๊ณ  ํ•˜๋‚˜์”ฉ ํ๋ฆ„์„ ํŒŒ์•…ํ•˜๊ธฐโ—โ—

์‚ฌ์šฉํ•œ ๋‚ด์žฅ ํ•จ์ˆ˜

โ—พ new Date()  = ํ˜„์žฌ ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„

โ—พ setInterval()   = (์‚ฌ์šฉํ•  ํ•จ์ˆ˜๋ช…, ์‹คํ–‰ ๊ฐ„๊ฒฉ(์‹œ๊ฐ„))        โœ” 1์ดˆ๋Š” 1000 ์œผ๋กœ ํ‘œ๊ธฐํ•œ๋‹ค.

โ—พ Math.floor()    = ๋‚ด๋ฆผํ•จ์ˆ˜     โœ” (5.1 = 5 ,  5.9 = 5)

โ—พ String()    = ์ •์ˆ˜๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ  

โ—พ padStart  = ์ง€์ •ํ•œ ๋นˆ๊ณต๊ฐ„ padding ๋„ฃ์–ด์ฃผ๊ธฐ (์ง€์ •ํ•œ ๊ณต๊ฐ„, ๋„ฃ์–ด์ค„ ์ž๋ฃŒํ˜•)      โœ” (2, "0")

 

1. html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" href="./css/styles.css">
</head>
<body>
    <h1 class="practice">ํฌ๋ฆฌ์Šค๋งˆ์Šค๊นŒ์ง€ ๋‚จ์€ ์‹œ๊ฐ„ </h1>
    <h2 id="clock"></h2>
    <script src="./js/index.js"></script>
</body>
</html>

 

 

2. js

const clocked = document.querySelector("#clock");

function setclocked() {
    const setdate = new Date("2024-12-25T00:00:00");
    const newdate = new Date();
    const distance = setdate - newdate;
    const seconds = String(Math.floor((distance / 1000) % 60)).padStart(2, "0");
    const minutes = String(Math.floor((distance / (1000 * 60)) % 60)).padStart(2, "0");
    const hours = String(Math.floor((distance / (1000 * 60 * 60)) % 24)).padStart(2, "0");
    const day = String(Math.floor(distance / (1000 * 60 * 60 * 24))).padStart(2, "0");
   
    clocked.innerText = `${day}์ผ ${hours}์‹œ ${minutes}๋ถ„ ${seconds}์ดˆ`;
}

setclocked();
setInterval(setclocked, 1000);

 

 

โœณ ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด์ž

 const setdate = new Date("2024-12-25T00:00:00");
 const newdate = new Date();

-> setdate ์— D-day ์ง€์ •ํ•  ๋‚ ์งœ๋ฅผ ์ ๋Š”๋‹ค.

-> newdate ์— ํ˜„์žฌ ๋‚ ์งœ๋ฅผ ์ ๋Š”๋‹ค. (Date() ์ธ์ˆ˜์— ์•„๋ฌด๊ฒƒ๋„ ์ ์ง€ ์•Š์œผ๋ฉด ํ˜„์žฌ ์‹œ๊ฐ„์„ ์ƒ์„ฑ ํ•ด์คŒ)

 

const distance = setdate - newdate;

-> ๋‘ ํ•จ์ˆ˜๋ฅผ ๋บ๋‹ˆ๋‹ค.

 ๐Ÿ’ก Date ํ•จ์ˆ˜๋กœ ์ƒ์„ฑํ•œ Date ๊ฐ์ฒด๋Š” ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 

 

const seconds = String(Math.floor((distance / 1000) % 60)).padStart(2, "0");   // ์ดˆ
const minutes = String(Math.floor((distance / (1000 * 60)) % 60)).padStart(2, "0");  // ๋ถ„
const hours = String(Math.floor((distance / (1000 * 60 * 60)) % 24)).padStart(2, "0");  // ์‹œ๊ฐ„
const day = String(Math.floor(distance / (1000 * 60 * 60 * 24))).padStart(2, "0");  // ์ผ

-> 1์ดˆ๋Š” 1000๋ฏธ๋ฆฌ ์ดˆ ์ž…๋‹ˆ๋‹ค.  ์ดˆ ๋‹จ์œ„์— 1000์œผ๋กœ ๋‚˜๋ˆ ์ฃผ๊ธฐ 

-> ๋ชจ๋“ˆ๋Ÿฌ(%) ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•ด์•ผ D-day ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.   ์‹œ๊ฐ„,๋ถ„,์ดˆ ๋‹จ์œ„์— ๋งž์ถฐ 60, 24 ๋กœ ๋‚˜๋ˆ ์ฃผ์ž.

(์„ค๋ช…ํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ๋ณต์žกํ•˜๋‹ค ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด ๋†“์€ ๋ถ„๋“ค์—๊ฒŒ ๊ฐ์‚ฌํ•˜์ž ์•Œ๋ฉด ๋‹ค์ณ ๐Ÿ˜‚)

-> Math.floor ๋Š” ๋‚˜๋ˆ„๊ธฐ ํ›„ ์†Œ์ˆ˜์ ์„ ์—†์• ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

-> padStart ๋ฅผ ์‚ฌ์šฉํ•ด์„œ 1,2,3 ์ด ์•„๋‹Œ 01,02,03 ์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ

-> "01","02","03" ๋ฌธ์ž์—ด์ด ๋˜๋ฏ€๋กœ String ์œผ๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.

 

clocked.innerText = `${day}์ผ ${hours}์‹œ ${minutes}๋ถ„ ${seconds}์ดˆ`;

-> ์•ž์—์„œ ๊ตฌํ•œ ๋ณ€์ˆ˜๋“ค์„ ๋„ฃ์–ด ํ™”๋ฉด์— ์ถœ๋ ฅํ•ด ์ฃผ๊ธฐ

 

 

์™„์„ฑ์ž‘