Skip to content

Instantly share code, notes, and snippets.

@zeraf29
Last active January 31, 2022 15:14
Show Gist options
  • Save zeraf29/3be46b13e078501120f4508d7c2c7a67 to your computer and use it in GitHub Desktop.
Save zeraf29/3be46b13e078501120f4508d7c2c7a67 to your computer and use it in GitHub Desktop.
NOMAD_CODER_CleanCode_Practice1
<html>
<script type="text/javascript" src="./TimeGap.js"></script>
<div class="js-clock"></div>
<script>
const SET_SECONDS = 1000;
const SET_MINUTES = SET_SECONDS * 60;
const SET_HOUR = SET_MINUTES * 60;
const SET_DAY = SET_HOUR * 24;
/*
* targetYear: 4자리 (ex: 2022)
* targetMonth: 1~12 (0~11 아님)
* targetDay: 1~31
*/
var showTargetTimeDiff = function(targetYear, targetMonth, taretDay){
const targetDiv = document.querySelector(".js-clock");
//Date 객체의 월 범위(0~11)를 맞추기 위해 targetmonth-1 처리함.
targetDiv.innerHTML = getTargetTimeDiff(targetYear, targetMonth-1, taretDay);
}
var getTargetTimeDiff = function(targetYear, targetMonth, targetDay) {
const targetDate = new Date(targetYear, targetMonth, targetDay);
const gapTime = targetDate.getTime() - Date.now()
const gapDay = Math.floor(gapTime / SET_DAY);
const gapHours = Math.floor((gapTime - (gapDay*SET_DAY)) / SET_HOUR);
const gapMinutes = Math.floor((gapTime % SET_HOUR) / SET_MINUTES);
const gapSeconds = Math.floor((gapTime % SET_MINUTES) / SET_SECONDS);
return new TimeGap(gapDay, gapHours, gapMinutes, gapSeconds).getTimeGapValues();
}
showTargetTimeDiff(2022, 12, 25);
setInterval(showTargetTimeDiff(2022, 12, 25), 1000);
</script>
</html>
class TimeGap{
constructor(day, hour, min, secs){
this.day = day;
this.hour = hour;
this.min = min;
this.secs = secs;
};
getTimeGapValues = () => `${this.stringWithPad(this.day)}d, ${this.stringWithPad(this.hour)}h, ${this.stringWithPad(this.min)}m, ${this.stringWithPad(this.secs)}s`;
stringWithPad = (number, targetLength = 2, padString = "0") => String(number).padStart(targetLength, padString)
}
@zeraf29
Copy link
Author

zeraf29 commented Jan 31, 2022

  1. 시간차이에 대해 별도 클래스를 생성하여 데이터 정리
  2. 중복 처리 부분을 별도 함수로 추출
  3. 계산과 출력문구 생성, 화면표시(innerHtml) 처리를 분리

*시간 차이 계산 부분을 별도 함수로 빼낼 수 없는지...궁금...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment