我只需要几小时、几分钟和几秒钟来倒计时

Posted

技术标签:

【中文标题】我只需要几小时、几分钟和几秒钟来倒计时【英文标题】:I need only hours, minutes and seconds for countdown 【发布时间】:2019-08-03 17:57:45 【问题描述】:

倒计时只需要时分秒,我搜索其他js 在谷歌,但没有找到我要找的。​​p>

我不需要几天。

// Set the date we're counting down to
var countDownDate = new Date("March 15, 2019 22:25: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 + "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);
&lt;p id="demo"&gt;&lt;/p&gt;

【问题讨论】:

只删除days + "d " + 位?我忽略了什么吗? 它只显示了几个小时,而不是显示 3 天的小时数,例如。 59 小时,显示 10 小时... 【参考方案1】:

// Set the date we're counting down to
var countDownDate = new Date("March 15, 2019 22:25: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)) + days * 24);
  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 = 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);
&lt;p id="demo"&gt;&lt;/p&gt;

更新了代码 sn-p。

var hours = Math.floor(((距离 % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) + 天 * 24); document.getElementById("demo").innerHTML = hours + "h" + minutes + "m" + 秒 + "s";

【讨论】:

您只需添加计算天数的小时数,即如果您有 2 天,那么您需要添加 48,即 24*2 小时。 我的荣幸。 @BimalPariyar:如果你得到了预期的答案。你能把它标记为“作为正确答案”吗?【参考方案2】:

您可以使用以下代码:http://covelign.com/3bNa

<html>
<head></head>
<body>
<!-- Display the countdown timer in an element -->
<p id="demo"></p>

<script>
// 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);
  var hrs = (days*24)+hours;	

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = hrs + "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>
</body>
</html>

【讨论】:

以上是关于我只需要几小时、几分钟和几秒钟来倒计时的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript Javascript在几分钟和几秒内倒计时

自从我在swift / xcode中关闭应用程序后几秒钟过去了?

几秒钟后如何停止计时器

几小时和几分钟的 jQuery 输入掩码

NSTimer 几秒钟后停止

创建一个以用户输入开头的java gui倒计时器