main
1const timer = document.getElementById('timer');
2const epoch = document.getElementById('epoch');
3const clock = document.getElementById('clock');
4const date = document.getElementById('date');
5const week = document.getElementById('week');
6const updateInterval = 1000;
7const start = Temporal.Now.zonedDateTimeISO();
8
9function clockNumber(s) {
10 return String(s).padStart(2,'0')
11}
12
13function formatDuration(seconds) {
14 if (seconds < 0) return "0s";
15
16 const units = [
17 { label: "d", value: 86400 }, // Days
18 { label: "h", value: 3600 }, // Hours
19 { label: "m", value: 60 }, // Minutes
20 { label: "s", value: 1 } // Seconds
21 ];
22
23 let result = "";
24 for (const unit of units) {
25 if (seconds >= unit.value) {
26 const count = Math.floor(seconds / unit.value);
27 result += `${count}${unit.label}`;
28 seconds %= unit.value;
29 }
30 }
31
32 return result || "0s";
33}
34
35function updateClock() {
36 const now = Temporal.Now.zonedDateTimeISO();
37 const sinceRefresh = now.epochSeconds - start.epochSeconds;
38
39 const formatted_timer = `${formatDuration(sinceRefresh)}`
40 const formatted_epoch = `${now.epochSeconds}`;
41 const formatted_now = `${clockNumber(now.hour)}:${clockNumber(now.minute)}`;
42 const formatted_date = `${now.toPlainDate().toString()}`;
43 const formatted_week = `week: ${now.weekOfYear}`;
44
45 epoch.innerText = formatted_epoch;
46 timer.innerText = formatted_timer;
47 if (clock.innerText != formatted_now) {
48 clock.innerText = formatted_now;
49 date.innerText = formatted_date;
50 week.innerText = formatted_week;
51 document.title = formatted_now;
52 }
53 setTimeout(updateClock, updateInterval);
54}
55
56updateClock();