๐Ÿ‘ฉ๐Ÿผโ€๐Ÿ’ป๋ฐ”๋‹๋ผ JS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ :#5.1-5.3

change upยท2023๋…„ 6์›” 12์ผ
3
post-custom-banner

ํ•™์Šต์ฃผ์ œ : clock๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel = "stylesheet" href="css/app.css">
    <title>Momentum</title>
</head>
<body>
    <form class="hidden" id="login-form">
    <input type="text" />
    <button id="button">Log In</button>
    <a href="https://nomadcoders.co/" class="hidden">Go nomad!</a>
    </form>
    <h2 id ="clock">00:00:00</h2>
    <h1 id="greeting" class="hidden"></h1>
    <script src="js/clock.js"></script>
    <script src="js/greeting.js"></script>
</body>
</html>
const clock = document.querySelector("h2#clock")
//interval : ๋งค๋ฒˆ ์ผ์–ด๋‚˜์•ผ ํ•˜๋Š” ๋ฌด์–ธ๊ฐ€๋ฅผ ๋งํ•œ๋‹ค.
function getClock() {
   const date = new Date();
   const hours = String(date.getHours()).padStart(2, "0");
   const minutes = String(date.getMinutes()).padStart(2, "0");
   const seconds = String(date.getSeconds()).padStart(2, "0");}
/*padStart() : ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ถœ๋ ฅ๋  ์ˆ˜ ์žˆ๋„๋ก ์•ž์ชฝ์— ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•œ๋‹ค. <->padEnd()
"1",padStart(2,"0") : ๋‘ ์ž๋ฆฌ์ˆ˜๊ฐ€ ์•„๋‹๊ฒฝ์šฐ์— ์•ž์— '0'์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
String(num) : number -> string์œผ๋กœ ๋ฐ”๊ฟ”์คŒ
   clock.innerText = `${hours}:${minutes}:${seconds}`;
date์˜ ๊ธฐ๋Šฅ
1)new Date() : //Mon Jun 12 2023 18:03:10 ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ๋‚ ์งœ๋ฅผ ๋‚˜ํƒ€๋ƒ„
2)date.getHours :ํ˜„์žฌ์‹œ๊ฐ„
3)date.getMinutes :๋ถ„
4)date.getSeconds:์ดˆ
2+3+4๋ฅผ ํ•ด์„œ ๋งค์ดˆ๋งˆ๋‹ค ์‹œ๊ฐ„์„ ๋‚˜ํƒ€๋‚ด๋Š” Date object๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.*/
getClock();
//์›น์‚ฌ์ดํŠธ๊ฐ€ ์‹คํ–‰๋˜์ž๋งˆ์ž ๋ฐ”๋กœ ์‹œ๊ณ„๊ธฐ๋Šฅ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ˜ธ์ถœํ•œ๋‹ค.
setInterval(getClock, 1000);
//1์ดˆ๋งˆ๋‹ค ํ˜ธ์ถœ์ด ์ง„ํ–‰๋œ๋‹ค.
profile
์ƒˆ์‹น์ด
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€