利用原生js实现倒计时,注意Date()函数中表示月份的参数介于 0 到 11 之间。也就是说,如果希望把月设置为 2 月,则参数应该是 1。
<script type="text/javascript">
window.onload = function () {
var oDiv1 = document.getElementById('div1')
var oDiv2 = document.getElementById('div2')
var oDiv3 = document.getElementById('div3')
function timeGo() {
var now = new Date();
var future = new Date(2020, 8, 28, 10, 35, 0)
// 相减得到的是毫秒,需要除以1000得到秒
var lefts = parseInt((future - now) / 1000)
var day = parseInt(lefts / 86400)
var hour = parseInt((lefts % 86400) / 3600)
var min = parseInt(((lefts % 86400) % 3600) / 60)
var sec = lefts % 60
oDiv1.innerHTML = now;
oDiv2.innerHTML = future;
oDiv3.innerHTML = day + "天 "+" "+ +todou(hour) + ":" + todou(min) + ":" + todou(sec);
}
timeGo();
setInterval(timeGo,1000);
// 按标准格式显示时间00:00:00,否则当时、分、秒的数字小于10时,会以单数字形式表现0.0.0.
function todou(num) {
if (num < 10) {
return '0' + num;
}
else {
return num;
}
}
}
</script>
</head>
<body>
<div id="div1"></div>
<div id='div2'></div>
<div id='div3' style="color:red;"></div>
</body>
</html>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有