<h1></h1>
<script>
var myh1 = document.querySelector("h1");
go();
setInterval(go, 1000);
function go() {
//过年倒计时
//现在的时间
var now = new Date();
// 过年时间(未来)
var future = new Date(2021, 2, 12);
// 过年时间 - 现在时间
// 毫秒 =>秒
var myt = (future - now) / 1000;
// 秒转天
var myd = Math.floor(myt / (24 * 60 * 60));
// 秒转小时
var myh = Math.floor(myt % (24 * 60 * 60) / (60 * 60));
// 秒转分
var mym = Math.floor(myt % 86400 % 3600 / 60);
// 秒
var mys = Math.floor(myt % 60);
var str = `距离过年还剩:${myd}天${fn(myh)}时${fn(mym)}分${fn(mys)}秒`
myh1.innerText = str;
}
function fn(num) {
if (num < 10) {
return "0" + num;
} else {
return num;
}
}
</script>
原生js,距离过年倒计时
最新推荐文章于 2023-01-12 10:44:34 发布