-2

This code basically puts out a countdown. The problem is, i would like it to be put out as D/HH/MM/SS as for example 1 day 05 hours 20 minutes 02 seconds, but i'm pretty new to coding, so i wonder if somebody could show/help me what is the right way to do it.

Thanks in advance!

Blockquote

<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {

// Get todays date and time

  var now = new Date().getTime();

// Find the distance between now an 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);

// Display the result in the element with id="demo"

  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

// If the count down is finished, write some text

  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Dan
  • 11
  • 1

1 Answers1

0

Sorry, I did not read your answer carefully enough the 1st time.

Details here about padStart a new language feature and an old work around (repeated below).

function pad(num, size) {
//num, string to be padded with leading zeros
//size, size of result after padding
//pads up to 10 characters  
    var s = "000000000" + num;
    return s.substr(s.length-size);
}

var test='1';
console.log(pad('123456789',10));  //pads up to 10 char, result '0123456789'
console.log(pad(test,2));  //pads up to 2 char, result '01'
JohnC
  • 2,687
  • 1
  • 22
  • 30