-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
30 lines (22 loc) · 1.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function updateTimer() {
let timeElement = document.getElementById("time");
let countUpDate = new Date("2022-08-06T09:26:00Z").getTime();
setInterval(() => {
let now = new Date().getTime();
let timeDiff = now - countUpDate;
let days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
timeDiff %= (1000 * 60 * 60 * 24); // simplify with %=
let hours = Math.floor(timeDiff / (1000 * 60 * 60));
timeDiff %= (1000 * 60 * 60); // simplify with %=
let minutes = Math.floor(timeDiff / (1000 * 60));
timeDiff %= (1000 * 60); // simplify with %=
let seconds = Math.floor(timeDiff / 1000);
let timeString = days.toString().padStart(2, "0") + "d "
+ hours.toString().padStart(2, "0") + "h "
+ minutes.toString().padStart(2, "0") + "m "
+ seconds.toString().padStart(2, "0") + "s";
// use padStart() to format the time values
timeElement.innerHTML = timeString;
}, 1000);
}
updateTimer();