gpt4 book ai didi

javascript - 在 HH :MM:SS instead of H:M:S in Javascript 中显示倒计时器

转载 作者:行者123 更新时间:2023-11-28 17:22:49 25 4
gpt4 key购买 nike

我设法以 H:M:S 格式显示倒计时器。

请问如何将其显示为 HH:MM:SS 格式?例如,假设 300 小时 1 分 1 秒,它将显示为 300:01:01 而不是 300:1:1 。

这就是我到目前为止所得到的。

// Set the date we're counting down to
var countDownDate = new Date("Aug 31, 2019 22:55:00").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 hours = Math.floor(distance / (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 = hours + " : "
+ minutes + " : " + seconds + "";

// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
<p id="demo"></p>

最佳答案

测试小于 10 的值并附加前导零

// Set the date we're counting down to
var countDownDate = new Date("Aug 31, 2019 22:55:00").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 hours = Math.floor(distance / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

if (hours < 10) hours = '0'+ hours;
if (minutes < 10) minutes = '0'+ minutes;
if (seconds < 10) seconds = '0'+ seconds;

// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = hours + " : "
+ minutes + " : " + seconds + "";

// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
<p id="demo"></p>

正如 kamoroso94 在评论中提到的,您还可以使用 padstart()

// Set the date we're counting down to
var countDownDate = new Date("Aug 31, 2019 22:55:00").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 hours = Math.floor(distance / (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 = hours.toString().padStart(2, '0') + " : "
+ minutes.toString().padStart(2, '0') + " : " + seconds.toString().padStart(2, '0') + "";

// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
<p id="demo"></p>

关于javascript - 在 HH :MM:SS instead of H:M:S in Javascript 中显示倒计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52118323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com