jquery - 如何将数字格式化为小数点前始终为两位数
问题描述
我已经建立了一个倒计时时钟,我需要分钟和秒来始终显示两位数。目前时钟运行完美,但当秒数和分钟数低于 2 位数时,它只显示个位数。(就像 10 以下的任何东西一样。)是否有 jQuery .format() 或类似的东西来格式化我的输出数字?
这是我的 JS:
// Set the date we're counting down to
var countDownDate = new Date("March 27, 2019 00: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);
// Output the result in an element with id="demo"
document.getElementById("countdown").innerHTML = '<div class="timer-wrapper"><div class="time">' + days + ':</div><span class="text">DAYS</span></div><div class="timer-wrapper"><div class="time">' + hours + ':</div><span class="text">HOURS</span></div><div class="timer-wrapper"><div class="time">' +
minutes + ':</div><span class="text">MINUTES</span></div><div class="timer-wrapper"><div class="time">' + seconds + '</div><span class="text">SECONDS</span></div>';
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
这是我的 HTML:
<div class="countdown-timer-wrapper">
<div class="timer" id="countdown"></div>
</div>
提前致谢。:)
解决方案
使用("0" + number).slice(-2)
, 格式化数字
试试下面的代码片段
// Set the date we're counting down to
var countDownDate = new Date("March 27, 2019 00: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 = ("0" +Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))).slice(-2);
var minutes = ("0" + Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60))).slice(-2);
var seconds = ("0" + Math.floor((distance % (1000 * 60)) / 1000)).slice(-2);
// Output the result in an element with id="demo"
document.getElementById("countdown").innerHTML = '<div class="timer-wrapper"><div class="time">' + days + ':</div><span class="text">DAYS</span></div><div class="timer-wrapper"><div class="time">' + hours + ':</div><span class="text">HOURS</span></div><div class="timer-wrapper"><div class="time">' +
minutes + ':</div><span class="text">MINUTES</span></div><div class="timer-wrapper"><div class="time">' + seconds + '</div><span class="text">SECONDS</span></div>';
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
<div class="countdown-timer-wrapper">
<div class="timer" id="countdown"></div>
</div>
推荐阅读
- python - 调用gensim时出现异常?
- java - JAVA/连接超时与套接字绑定
- sql - Oracle SQL 11g:如何更新列,使每个单词的首字母大写?
- google-apps-script - 在未来日期发送自动电子邮件
- mysql - Hibernate 本机查询不会创建自动递增的主键
- azure - 使用 Azure Runbook 将文件从本地计算机或网络驱动器上传到 Azure 文件共享或 Blob 存储
- solr - solr 查询中的 AND 返回更多结果
- azure-active-directory - 服务主体的对象 ID 何时更改
- postgresql - PostgreSQL 在表的信息模式中显示错误的序列详细信息
- python - 获取 Instagram 帖子的点赞者列表 - Python 和 Selenium