Javascript/GMA(2302~)

23.04.19 자바스크립트 내장객체(*Date 객체) - 특정 날로 부터 몇일이 흘렀는지 출력하기(d-day)

hyerin1201 2023. 4. 19. 22:46

HTML

<body>
  <h1>만 보 걷기</h1>
  <p>
    <span id="result"></span>
    일 연속 달성!</p>
</body>

 

CSS

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 100vh;
}

h1 {
  margin-bottom: 20px;
  border-bottom: 1px solid #999;
  padding-bottom: 5px;
}
#result {
  font-size: 1.5em;
  font-weight: bold;
  color: tomato;
}

 

Javascript

const today = new Date();
const firstDay = new Date("2023-02-28");
const result = document.querySelector("#result");

let passedTime = today.getTime() - firstDay.getTime();
let passedDay = Math.round(passedTime / (24 * 60 * 60 * 1000));

result.innerText = passedDay;