xxxxxxxxxx
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2024 15:37:25").getTime();
// Update the count down 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 count down 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 an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
//example: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_countdown
xxxxxxxxxx
<!DOCTYPE html>
<html>
<head>
<title>Countdown Timer</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="timer">00:00:00</div>
<script>
$(document).ready(function() {
// Set the date and time to countdown to (e.g., New Year's Eve)
var countdownDate = new Date("December 31, 2022 23:59:59").getTime();
// Update the countdown every second
var countdown = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Calculate the remaining time
var remainingTime = countdownDate - now;
// Calculate hours, minutes, and seconds
var hours = Math.floor((remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((remainingTime % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((remainingTime % (1000 * 60)) / 1000);
// Display the countdown
$('#timer').text(padZero(hours) + ':' + padZero(minutes) + ':' + padZero(seconds));
// If the countdown is finished, stop the timer
if (remainingTime < 0) {
clearInterval(countdown);
$('#timer').text('Countdown expired');
}
}, 1000);
// Function to pad zeros to single-digit numbers
function padZero(number) {
return (number < 10 ? '0' : '') + number;
}
});
</script>
</body>
</html>
xxxxxxxxxx
var timer2 = "5:01";
var interval = setInterval(function() {
var timer = timer2.split(':');
//by parsing integer, I avoid all extra string processing
var minutes = parseInt(timer[0], 10);
var seconds = parseInt(timer[1], 10);
--seconds;
minutes = (seconds < 0) ? --minutes : minutes;
if (minutes < 0) clearInterval(interval);
seconds = (seconds < 0) ? 59 : seconds;
seconds = (seconds < 10) ? '0' + seconds : seconds;
//minutes = (minutes < 10) ? minutes : minutes;
$('.countdown').html(minutes + ':' + seconds);
timer2 = minutes + ':' + seconds;
}, 1000);