如何在JavaScript Countdown Timer问题中添加0

时间:2019-03-19 21:10:07

标签: javascript countdowntimer jquery-countdown

我找不到任何演示/小提琴/笔。您可以帮我一个链接吗?

问题:

我在网站上有一个倒数计时器。根据以下代码-

// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 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 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 + "days " + 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);
<!DOCTYPE HTML>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>

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

</body>

</html>

但是当计数器到 1days 9h 4m 3s 时。但是如何更改它,例如“ 01day 09h 04m 03s ” 在一位数字前添加“ 0”。同样,当天数为0或1时,文本之后为 day 。就像0day,01day但02days。

谢谢。

0 个答案:

没有答案
相关问题