HTML Structure
First, create the basic structure in HTML. This includes creating div elements for displaying the countdown timer.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Countdown Timer</title>
<style>
.countdown-container {
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
}
.countdown-item {
margin: 0 10px;
text-align: center;
font-size: 2em;
}
.countdown-item span {
display: block;
font-size: 0.5em;
}
</style>
</head>
<body>
<h1>Countdown to Event</h1>
<div class="countdown-container">
<div class="countdown-item" id="days">0<span>Days</span></div>
<div class="countdown-item" id="hours">0<span>Hours</span></div>
<div class="countdown-item" id="minutes">0<span>Minutes</span></div>
<div class="countdown-item" id="seconds">0<span>Seconds</span></div>
</div>
<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2030 15:37:25").getTime();
// Update the countdown every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the countdown date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in the respective elements
document.getElementById("days").innerHTML = days + "<span>Days</span>";
document.getElementById("hours").innerHTML = hours + "<span>Hours</span>";
document.getElementById("minutes").innerHTML = minutes + "<span>Minutes</span>";
document.getElementById("seconds").innerHTML = seconds + "<span>Seconds</span>";
// If the countdown is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("days").innerHTML = "0<span>Days</span>";
document.getElementById("hours").innerHTML = "0<span>Hours</span>";
document.getElementById("minutes").innerHTML = "0<span>Minutes</span>";
document.getElementById("seconds").innerHTML = "0<span>Seconds</span>";
}
}, 1000);
</script>
</body>
</html>
Explanation:
- HTML Structure:
- A simple structure with a heading and a container for the countdown timer.
- Each unit of time (days, hours, minutes, seconds) is wrapped in a
divwith a uniqueidfor easy access via JavaScript.
- CSS Styling:
- Basic styling to center the countdown timer and style the text.
- You can customize this part as needed.
- JavaScript Functionality:
- The
countDownDatevariable holds the target date and time. - A function is set to update the countdown every second using
setInterval. - Time calculations determine the remaining days, hours, minutes, and seconds.
- The results are injected into the respective HTML elements.
- If the countdown reaches zero, it stops updating and sets all values to zero.