-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (42 loc) · 1.75 KB
/
Copy pathscript.js
File metadata and controls
50 lines (42 loc) · 1.75 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
const persone = {
'DC': { data: '1975-04-05', testo: 'Sabato 5 aprile 1975' },
'BM': { data: '1975-04-23', testo: 'Mercoledì 23 aprile 1975' },
};
const params = new URLSearchParams(window.location.search);
let chiaveRichiesta = params.get('chi')?.toUpperCase();
if (!chiaveRichiesta) {
const urlIntero = window.location.pathname.toUpperCase();
chiaveRichiesta = Object.keys(persone).find(chiave => urlIntero.includes('/' + chiave)) || 'DC';
}
const chi = persone[chiaveRichiesta] ? chiaveRichiesta : 'DC';
const profilo = persone[chi];
const start = new Date(`${profilo.data}T00:00:00`);
document.getElementById('title').textContent = `Quanto tempo ha vissuto ${chi}?`;
document.getElementById('info').textContent = profilo.testo;
function update() {
const now = new Date();
let anni = now.getFullYear() - start.getFullYear();
let mesi = now.getMonth() - start.getMonth();
let giorni = now.getDate() - start.getDate();
let ore = now.getHours() - start.getHours();
if (ore < 0) { ore += 24; giorni--; }
if (giorni < 0) {
const mesePrec = new Date(now.getFullYear(), now.getMonth(), 0).getDate();
giorni += mesePrec;
mesi--;
}
if (mesi < 0) { mesi += 12; anni--; }
if (anni < 0) {
document.getElementById('anni').textContent = "-";
document.getElementById('mesi').textContent = "-";
document.getElementById('giorni').textContent = "-";
document.getElementById('ore').textContent = "-";
return;
}
document.getElementById('anni').textContent = anni;
document.getElementById('mesi').textContent = mesi;
document.getElementById('giorni').textContent = giorni;
document.getElementById('ore').textContent = ore;
}
update();
setInterval(update, 1000);