Countdown Timer
/* styles.css */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
padding: 0;
}
.container {
text-align: center;
}
h1 {
color: #333;
}
#timer {
margin-bottom: 20px;
}
input[type="datetime-local"] {
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid #ccc;
}
button {
background-color: #3498db;
color: #fff;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
}
#countdown-display {
font-size: 24px;
}
// scripts.js
let countdownInterval;
function startCountdown() {
const datetimeInput = document.getElementById('datetime-input').value;
const targetDate = new Date(datetimeInput).getTime();
if (isNaN(targetDate)) {
alert('Please enter a valid date and time.');
return;
}
countdownInterval = setInterval(function() {
const currentDate = new Date().getTime();
const timeRemaining = targetDate - currentDate;
if (timeRemaining <= 0) {
clearInterval(countdownInterval);
document.getElementById('countdown-display').innerHTML = 'Countdown expired';
} else {
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);
document.getElementById('countdown-display').innerHTML = `${days}d ${hours}h ${minutes}m ${seconds}s`;
}
}, 1000);
}
No comments:
Post a Comment