美文网首页
数字时钟

数字时钟

作者: 编程师小刘 | 来源:发表于2016-12-02 14:36 被阅读0次
<style>

body{

background: #000;

color: #fff;

text-align: center;

font-size:100px;

}

</style>

<script>

function toDou(n) {

return n < 10 ? '0' + n : '' + n;

}

window.onload = function(){

var oDiv = document.getElementById('div1');

var oDiv2 = document.getElementById('div2');

function clock(){

//获取当前时间

var oDate = new Date();

var h = oDate.getHours();

var m = oDate.getMinutes();

var s = oDate.getSeconds();

var Y = oDate.getFullYear();

var M = oDate.getMonth() + 1;

var D = oDate.getDate();

var W = oDate.getDay();

//转换 W 星期,为汉字

switch (W){

case 1:

W = '一';

break;

case 2:

W = '二';

break;

case 3:

W = '三';

break;

case 4:

W = '四';

break;

case 5:

W = '五';

case 6:

W = '六';

break;

case 0:

W = '日';

break;

}

//设置div的内容,并且完成了补0

oDiv.innerHTML = toDou(h) + ':' + toDou(m) + ':' + toDou(s);

oDiv2.innerHTML = Y + '年'+ M +'月'+ D +'日 星期' + W;

}

clock();

setInterval(clock,1000);

};

</script>

</head>

<body>

<div>北京时间</div>

<br />

<div id="div1">17:18:20</div>

<br />

<div id="div2"></div>

</body>

相关文章

网友评论

      本文标题:数字时钟

      本文链接:https://www.haomeiwen.com/subject/bxqqmttx.html