๐ŸŽจ Frontend/JavaScript

๐ŸฆVanilla JS โ‘ค ๊ฐ์ฒด - (2).3 ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด : Date, D-DAY ํ”„๋กœ๊ทธ๋žจ

kongmi 2023. 3. 29. 14:35

๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ๋‹ค๋ฃจ๋Š” Date ๊ฐ์ฒด

const date = new Date();
console.log(date);
  • Date ๊ฐ์ฒด๋Š” ์šด์˜์ฒด์ œ์—์„œ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

์‹œ๊ฐ„ ์ง์ ‘ ์„ธํŒ…ํ•˜๋Š” ๋ฒ•

์ธ์Šคํ„ด์Šค ๋งŒ๋“ค๊ธฐ

  • ์›”(ๆœˆ)์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘, GMT ๊ธฐ์ค€ ์‹œ๊ฐ„์œผ๋กœ ์„ค์ • ๋จ.
const date1 = new Date(2022, 11, 27, 18, 30, 50);
console.log(date1);
// ์›ํ•˜๋Š” ๋‹ฌ๋กœ ์ž…๋ ฅ ๋จ
const date2 = new Date("2022-12-25/18:30:50");
console.log(date2);

Date ๊ฐ์ฒด ๋ฉ”์†Œ๋“œ

let myDate = new Date();
console.log(myDate.getFullYear()); // 2023 (ํ˜„์žฌ ์—ฐ๋„ ์ถœ๋ ฅ)
console.log(myDate.getMonth()+1); // 3 (์›”(ๆœˆ)์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 1 ์ถ”๊ฐ€)
console.log(myDate.getDate()); // 29
console.log(myDate.getDay()); // 3 (ํ˜„์žฌ ์š”์ผ ์ถœ๋ ฅ - ์›”์š”์ผ(1)๋ถ€ํ„ฐ ์‹œ์ž‘)
console.log(myDate.getHours()); // ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅ
console.log(myDate.getMinutes()); // ๋ถ„ ์ถœ๋ ฅ
console.log(myDate.getSeconds()); // ์ดˆ ์ถœ๋ ฅ
console.log(myDate.getTime()); // 1970.01.01 00:00 ๋ถ€ํ„ฐ ์ง€๊ธˆ๊นŒ์ง€ ๊ฒฝ๊ณผ ์‹œ๊ฐ„(๋ฐ€๋ฆฌ์ดˆ) ๋ฐ˜ํ™˜

D-DAY ๊ณ„์‚ฐ ํ”„๋กœ๊ทธ๋žจ

  • ์ง€๊ธˆ ๋‹ค๋‹ˆ๊ณ  ์žˆ๋Š” ํ•™์› ์ˆ˜๋ฃŒ ๋‚ ์งœ๊นŒ์ง€ ๋ช‡ ์ผ ๋‚จ์•˜๋Š”์ง€ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. (2023-07-25)
  • 9n๋…„์ƒ๋“ค์ด๋ผ๋ฉด ์นœ์ˆ™ํ•˜์‹ค ์ถ”์–ต์˜ ์•„๊ตฌ๋ชฌ์„ ๋ฐฐ๊ฒฝ์œผ๋กœ ๋‘์—ˆ์–ด์š” (ใ…‹ใ…‹ใ…‹)
  • ์›๋ž˜๋Š” ์˜ค๋Š˜ ๋‚ ์งœ๋กœ๋งŒ ๋”ฑ ๋ฐ•์•„์„œ ๋งŒ๋“ค์—ˆ์—ˆ๋Š”๋ฐ, ๋„ˆ๋ฌด ์œ ์—ฐ์„ฑ์ด ์—†๋Š” ๋“ฏ ํ•ด์„œ ์ง์ ‘ ์‚ฌ์šฉ์ž๊ฐ€ ๋‚ ์งœ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

HTML

<!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">
  <title>Document</title>
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
    }
    h1 {
      text-align: center;
      padding: 20px;
    }
    #box {
      width: 400px;
      height: 400px;
      position: relative;
      margin: 0 auto;
      border-radius: 80px;
      border: 1px solid #B0A8B9;
    }
    #box::before {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      opacity: 0.5;
      background-size:cover;
      background-repeat: no-repeat;
      background-image: url("./230329_1_1_์•„๊ตฌ๋ชฌ.png");
    }
    #box #value {
      text-align: center;
      font-size: 4rem;
      font-weight: bold;
      top: 150px;
      left: 10px;
      position: relative;
    }
    #value:hover {
      transform: scale(2,2);
      color: #C34A36;
    }
    .input {
      text-align: center;
      padding: 10px 0 40px 0;
    }
  </style>
</head>
<body>
  <h1>ํ•™์› ์ˆ˜๋ฃŒ D-DAY</h1>
  <div class="input">
    <label for="text">๊ธฐ์ค€ ๋‚ ์งœ (๋…„-์›”-์ผ) </label>
    <input type="text" id="date">
  <button onclick="dDayCalculate()">๊ณ„์‚ฐํ•˜๊ธฐ!</button>
  </div>
  <div id="box">
    <p id="value"></p>
  </div>
  <script src="./230329_1_2_๋””๋ฐ์ด๊ณ„์‚ฐ.js"></script>
</body>
</html>

JavaScript

function dDayCalculate() {
  const date = document.getElementById("date").value;
  const date1 = new Date(date);
  const endDate = new Date("2023-07-25");

  endDateReal = endDate.getTime();
  date1Real = date1.getTime();
  const dDay = endDateReal - date1Real;
  const dDayReal = Math.floor(dDay/(1000*60*60*24));
  document.getElementById("value").innerHTML = `${dDayReal}์ผ`;
}

ํ™”๋ฉด๊ตฌํ˜„