当前位置: 首页 > 知识库问答 >
问题:

我只需要小时,分钟和秒来倒数

牛兴安
2023-03-14

我只需要小时,分钟和秒的倒计时,我在谷歌上搜索其他的js,但没有找到我要找的。

我不需要几天。

js lang-js prettyprint-override">// 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);
<p id="demo"></p>

共有2个答案

年业
2023-03-14

你可以使用下面的代码: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>
施轶
2023-03-14
// 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);
<p id="demo"></p>
 类似资料:
  • 因此,我试图制作一个程序,可以将s从输入转换为h、m和s。到目前为止,我的代码如下所示: 好的,所以我必须将s,m,h初始化为0,因为如果不是的话,我在if语句中遇到了问题,所以我把它设置为0,因为我可以稍后更改:)好的。所以现在这个程序的问题是,如果我输入3603,我会得到这个输出:3603s=1H60M3603s,如果我输入3600,我会得到:3600s=1H60M3600s,但输出应该分别是

  • 我的函数返回时间,例如。如何将时间取整到最接近的一分钟,使变为并使变为。

  • 我刚刚开始学习java,我有一个问题,似乎有一个简单的答案。。。

  • 我是Python新手,遇到了这个问题。我想创建一个程序,用户输入秒数,并将其转换为天、小时、分钟和秒。然而,例如,我希望程序在100000秒内输出1天、3小时、46分钟、40秒。然而,mine也用小数输入计算结果,比如说1.157天,其他类别也一样。任何帮助都将不胜感激,非常感谢。

  • 我试图创建一个基于时间的倒计时钟。它不是基于current_dates。将被拉取的初始时间将来自一个单独的php文件。这将是一个基于浏览器的游戏。当有人点击按钮来启动这个脚本。它将检查是否满足某些要求,如果满足,那么这个脚本将启动。根据对象的级别,它将拉动该进行中级别的初始计时器。希望这有意义。无论如何,我基于我提供的第一个代码的计时器脚本。 此脚本仅占分钟和秒。我对它进行了修改,使其也包括了天和

  • 问题内容: 我需要将秒转换为“小时:分钟:秒”。 例如:“ 685”转换为“ 00:11:25” 我该如何实现? 问题答案: 您可以使用以下功能: