I have a simple countdown and I would like for there to always be 2 numbers for days, hours, minutes and seconds. For instance, if there's 1 day left, I want it to say 01
instead of 1
. How can I modify this code for that?
var countDownDate = new Date('Dec 23, 2019 12:00:00').getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
$('.countdown').html('<div>' + days + '<span>days</span></div> : <div>' + hours + '<span>hrs</span></div> : <div>'
+ minutes + '<span>min</span></div> : <div>' + seconds + '<span>sec</span></div>');
if (distance < 0) {
clearInterval(x);
$('.countdown').hide();
}
}, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="countdown"></diV>