获取系统时间 让时间动起来
程序员文章站
2022-05-24 14:14:07
...
js代码
function test1() {
var date = new Date();
var year = date.getFullYear(); //获取系统的年;
var month = date.getMonth()+1; //获取系统月份,由于月份是从0开始计算,所以要加1
var day = date.getDate(); // 获取系统日,
var hour = date.getHours(); //获取系统时,
var minute = date.getMinutes(); //分
minute = minute < 10 ? "0"+minute:minute;
var second = date.getSeconds(); //秒
second = second < 10 ? "0"+second:second;
var div1 = document.getElementById("div1");
div1.innerHTML = year + "年" + month + "月" + day + "日 " + hour + ":" + minute + ":" + second;
}
var label;
function test2(){
label = setInterval(test1, 1000);
}
function test3(){
clearInterval(label);
}
页面
<body>
<input type="button" onclick="test1()"value="显示时间">
<input type="button" onclick="test2()"value="让时间动起来">
<input type="button" onclick="test3()"value="暂停">
<div id="div1">
</div>
</body>