gpt4 book ai didi

javascript - 在 Javascript 倒计时器中创建两位数?

转载 作者:行者123 更新时间:2023-12-01 03:42:40 25 4
gpt4 key购买 nike

如果数字只剩下一位数字,我需要创建什么 JavaScript 才能使倒计时器始终为两位数。如果秒为 6,我希望它说 06,如果小时为 2,我希望它说 02,依此类推。

笔:http://codepen.io/zepzia/pen/MmoVJm

HTML

<link href="https://fonts.googleapis.com/css?family=Roboto:400,900" rel="stylesheet">

<body>
<div class="countdown-wrapper">
<div id="countdown-text">
<div class="timer">
<div id="daysTicker" class="countdown"></div>
<div class="counter-text">DAYS</div>
</div>
<div class="timer">
<div id="hoursTicker" class="countdown"></div>
<div class="counter-text">HOURS</div>
</div>
<div class="timer">
<div id="minsTicker" class="countdown"></div>
<div class="counter-text">MINS</div>
</div>
<div class="timer">
<div id="secsTicker" class="countdown"></div>
<div class="counter-text">SECS</div>
</div>
</div>
</div>
</body>

CSS

body {
background:url(http://4.bp.blogspot.com/_AQ0vcRxFu0A/S9shDGGyMTI/AAAAAAAAAYk/kn3WTkY2LoQ/s1600/IMG_0714.JPG);
background-size:cover;
background-position:center center;
background-attachment:fixed;
}

.countdown-wrapper {
position: relative;
height: 400px;
}

#countdown, #countdown-text {
font-family: 'Roboto', sans-serif;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.countdown {
font-weight: 900;
font-size: 142px;
color: #fff;
opacity: .7;
letter-spacing: -4px;
}

.counter-text {
font-weight: 900;
font-size: 40px;
color: black;
opacity: .8;
text-align: center;
}


.timer {
display: inline-block;
margin: 10px;
}

JS

// Set the date we're counting down to
var countDownDate = new Date("Oct 7, 2017 12:00: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 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("daysTicker").innerHTML = days;
document.getElementById("hoursTicker").innerHTML = hours;
document.getElementById("minsTicker").innerHTML = minutes;
document.getElementById("secsTicker").innerHTML = seconds;

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

最佳答案

您可以使用toLocaleString,并将最小数字设置为2,这样当只有一位数字时,它会在其前面加上0前缀

var x = 8;

console.log(x.toLocaleString(undefined,{minimumIntegerDigits: 2}));

var y = 12;

console.log(y.toLocaleString(undefined,{minimumIntegerDigits: 2}));

关于javascript - 在 Javascript 倒计时器中创建两位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43761047/

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