codice:
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Countdown Settimanale</title>
<style>
#countdown {
font-size: 24px;
text-align: center;
margin: 20px;
}
</style>
</head>
<body>
<div id="countdown"></div>
<script>
// Funzione per ottenere la data del prossimo sabato alle 10:00
function getNextSaturday() {
const now = new Date();
const dayOfWeek = now.getDay();
const daysUntilSaturday = dayOfWeek === 6 ? 7 : 6 - dayOfWeek;
const nextSaturday = new Date(now.getFullYear(), now.getMonth(), now.getDate() + daysUntilSaturday);
nextSaturday.setHours(10, 0, 0, 0);
return nextSaturday;
}
// Funzione per aggiornare il countdown
function updateCountdown() {
const now = new Date();
const nextSaturday = getNextSaturday();
const timeDifference = nextSaturday - now;
if (timeDifference <= 0) {
// Se la data è già passata, calcola la data per il prossimo sabato
const nextSaturdayNextWeek = new Date(nextSaturday);
nextSaturdayNextWeek.setDate(nextSaturdayNextWeek.getDate() + 7);
nextSaturdayNextWeek.setHours(10, 0, 0, 0);
// Calcola il nuovo tempo rimanente
const newTimeDifference = nextSaturdayNextWeek - now;
// Aggiorna la data del prossimo sabato
nextSaturday.setTime(nextSaturdayNextWeek.getTime());
// Richiama la funzione ogni secondo
setTimeout(updateCountdown, 1000);
}
const days = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeDifference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
document.getElementById('countdown').innerHTML = `${days} giorni, ${hours} ore, ${minutes} minuti`;
// Richiama la funzione ogni secondo
setTimeout(updateCountdown, 1000);
}
// Avvia il countdown quando la pagina è pronta
document.addEventListener('DOMContentLoaded', function() {
updateCountdown();
});
</script>
</body>
</html>