JSFiddle - React, Tailwind, and code Playground
by pphheerroonn
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Countdown Timer</title>
<style>
#countdown {
font-size: 24px;
font-family: 'Arial', sans-serif;
text-align: center;
margin-top: 50px;
}
</style>
</head>
<body>
<div id="countdown"></div>
<script>
// Set the target date and time
const targetDate = new Date("December 20, 2023 14:00:00").getTime();
// Update the countdown every second
const countdownInterval = setInterval(function() {
// Get the current date and time
const currentDate = new Date().getTime();
// Calculate the time remaining
const timeRemaining = targetDate - currentDate;
// Calculate days, hours, minutes, and seconds
const days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
// Display the countdown
document.getElementById("countdown").innerHTML =
days + " days " + hours + "h " + minutes + "m " + seconds + "s ";
// Check if the countdown has reached zero
if (timeRemaining <= 0) {
clearInterval(countdownInterval);
document.getElementById("countdown").innerHTML = "It's time!";
}
}, 1000);
</script>
</body>
</html>