๐จ 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}์ผ`;
}
ํ๋ฉด๊ตฌํ