Last active
January 31, 2022 15:14
-
-
Save zeraf29/3be46b13e078501120f4508d7c2c7a67 to your computer and use it in GitHub Desktop.
NOMAD_CODER_CleanCode_Practice1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
*시간 차이 계산 부분을 별도 함수로 빼낼 수 없는지...궁금...