JavaScript 案例19
Date 获取 时分秒
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Date</title>
</head>
<body>
<script>
var oDate = new Date();
alert(oDate.getHours());
alert(oDate.getMinutes());
alert(oDate.getSeconds());
</script>
</body>
</html>
JavaScript 案例20
数码时钟
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数码时钟</title>
<style>
body{
font-size: 50px;
background: forestgreen;
}
</style>
</head>
<body>
<div id="clock">
<img src="img/0.png" alt="">
<img src="img/0.png" alt="">:
<img src="img/0.png" alt="">
<img src="img/0.png" alt="">:
<img src="img/0.png" alt="">
<img src="img/0.png" alt="">
</div>
<script>
function toDou(n) {
if (n<10){
return "0"+n;
}else {
return ""+n;
}
}
var oDiv = document.getElementById("clock");
var aImg = oDiv.getElementsByTagName("img");
function clock() {
var oDate = new Date();
var str = toDou(oDate.getHours()) + toDou(oDate.getMinutes())+toDou(oDate.getSeconds());
for(var i = 0; i<aImg.length;i++){
aImg[i].src = 'img/'+str.charAt(i)+'.png';
}
}
setInterval(clock,1000);
clock();
</script>
</body>
</html>