Skip to content

Commit 67d5bc5

Browse files
committed
refactor: add formatTime helper
1 parent 8928b7e commit 67d5bc5

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

076-new year countdown/script.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const loading = document.getElementById("loading");
99
const nextYear = new Date().getFullYear() + 1;
1010
const newYearTime = new Date(`January 01 ${nextYear} 00:00:00`);
1111

12+
function formatTime(unit) {
13+
return unit < 10 ? "0" + unit : unit;
14+
}
15+
1216
function updateCountdown() {
1317
const currentTime = new Date();
1418
const difference = newYearTime - currentTime;
@@ -17,11 +21,9 @@ function updateCountdown() {
1721
const currentMinutes = Math.floor(difference / 1000 / 60) % 60;
1822
const currentSeconds = Math.floor(difference / 1000) % 60;
1923
days.innerText = currentDays;
20-
hours.innerText = currentHours < 10 ? "0" + currentHours : currentHours;
21-
minutes.innerText =
22-
currentMinutes < 10 ? "0" + currentMinutes : currentMinutes;
23-
seconds.innerText =
24-
currentSeconds < 10 ? "0" + currentSeconds : currentSeconds;
24+
hours.innerText = formatTime(currentHours);
25+
minutes.innerText = formatTime(currentMinutes);
26+
seconds.innerText = formatTime(currentSeconds);
2527
}
2628

2729
setTimeout(() => {

0 commit comments

Comments
 (0)