<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: rgb(128, 0, 0);
color: white;
text-align: center;
line-height: 50px;
}
.box1 {
width: 150px;
height: 50px;
background-color: green;
margin: 0 auto;
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
}
.box1 div {
width: 50px;
height: 50px;
color: white;
}
</style>
</head>
<body>
<div class="box">
<p>下班倒计时?天</p>
<div class="box1">
<div class="box2">? :</div>
<div class="box3">? :</div>
<div class="box4">?</div>
</div>
</div>
<script>
const div = document.querySelector("div")
// 1970.1.1毫秒
// 时间戳三种方式
// getTime() 必须实例化
// +new Date
// Date.now()
function timer(){
const p = document.querySelector('.box p')
const box2 = document.querySelector('.box2')
const box3 = document.querySelector('.box3')
const box4 = document.querySelector('.box4')
const future = +new Date('2023-6-6 00:00:00')
const now = +new Date()
const count = (future - now) / 1000
let d = parseInt(count / 60 / 60 / 24)//总天数
let h = parseInt(count / 60 / 60 % 24)//时
h = h < 10 ? '0' + h : h
let m = parseInt(count / 60 % 60)//分
m = m < 10 ? '0' + m : m
let s = parseInt(count % 60)//秒
s = s < 10 ? '0' + s : s
p.innerHTML = `下班倒计时${d}天`
box2.innerHTML = `${h}:`
box3.innerHTML = `${m}:`
box4.innerHTML = `${s}`
// div.innerHTML = `还剩下${d}天${h}时${m}分${s}秒`
// console.log(d,h,m,s);
}
timer()
setInterval(timer,1000)
</script>
</body>
</html>
JavaScript下班倒计时【基础】
最新推荐文章于 2024-11-19 15:20:01 发布
本文深入探讨了如何使用JavaScript实现下班倒计时功能,涵盖了基础的日期和时间处理,以及定时器的运用。通过实例代码解析,帮助开发者掌握这一实用技巧。
1383

被折叠的 条评论
为什么被折叠?



