JS date()方法的使用
data()
var d=new Date();
console.log(d)
结果是 Wed Feb 08 2023 10:30:12 GMT+0800 (中国标准时间)
如何显示 当前时间点入 时分秒10:30:12
let d=new Date();
let time = d.toLocaleTimeString();
console.log(time);
结果就是当前时间;
做一个钟表的效果,通过 setInterval()方法;
html
<div class="box">
<p> 时钟</p>
<div id="demo">显示当前时间</div>
<div class="bu">
<input type="button" value="暂停" onclick="stop()">
<input type="button" value="继续" onclick="go()">
</div>
</div>
JS
let myTime = setInterval(function (){
let d = new Date();
let time = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = time;
},1000)
function stop(){
clearInterval(myTime);
}
function go(){
setInterval(function (){
let d = new Date();
let time = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = time;
},1000)
}
CSS
.box{
width: 200px;
height: 200px;
background: gainsboro;
box-shadow: 1px 1px 5px 2px black;
margin: 50px auto;
text-align: center;
padding-top: 20px;
}
.bu{
margin-top: 20px;
}
效果图: