-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown.js
More file actions
63 lines (49 loc) · 1.72 KB
/
Copy pathcountdown.js
File metadata and controls
63 lines (49 loc) · 1.72 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function calculateCountdown(endDateString) {
//la data di fine
// ISO date 03/02/2023, 02/03/2023, 2023-02-03T12:54
let endDate = new Date(endDateString);
//la data d'inizio
let startDate = new Date();
let timeDifference = (endDate - startDate) / 1000;
let days = Math.floor(timeDifference / 86400);
timeDifference -=
days * 86400; /* timeDifference = timeDifference - days * 86400; */
let hours = Math.floor(timeDifference / 3600) % 24;
timeDifference -= hours * 3600;
let minutes = Math.floor(timeDifference / 60) % 60;
timeDifference -= minutes * 60;
let seconds = Math.floor(timeDifference) % 60;
return {
days,
hours,
minutes,
seconds,
};
}
const pad = (unit) => {
return String(unit).padStart(2, "0");
};
/* console.log({days, hours, minutes, seconds}); //{} Creo i dizionari */
function updateCountdownTimer() {
let timeLeft = calculateCountdown("2023-02-05T00:00");
let elements = {
days: document.querySelector("#days"),
hours: document.querySelector("#hours"),
minutes: document.querySelector("#minutes"),
seconds: document.querySelector("#seconds"),
};
elements.days.innerText = pad(timeLeft.days) + "D" /* .padEnd(4, " D"); */
elements.hours.innerText = pad(timeLeft.hours) + "H" /* .padEnd(4, " H"); */
elements.minutes.innerText = pad(timeLeft.minutes) + "M" /* .padEnd(4, " M"); */
elements.seconds.innerText = pad(timeLeft.seconds)+ "S" /* .padEnd(4, " S"); */
}
document.addEventListener("DOMContentLoaded", function (event) {
updateCountdownTimer();
setInterval(() => {
updateCountdownTimer();
}, 1000);
console.log("pad");
});
/* calculateCountdown("2023-02-05T00:00");
updateCountdownTimer(); */
/* console.log("Eccomi!"); // */