JS时钟

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>无标题文档</title>
 6 
 7 <script language="javascript">
 8 window.onload=function(){
 9     timer();
10 }
11 function timer(){
12     var oDiv=document.getElementById("div1");
13     var today=new Date();//当前时间
14     //console.log(today);
15     var year=today.getFullYear();//
16     var month=today.getMonth()+1;//
17     var date=today.getDate();//
18     var week=today.getDay();//星期
19     var hour=today.getHours();//小时
20     var minute=today.getMinutes();//分钟
21     var second=today.getSeconds();//
22     switch(week){
23         case 0:week="日";break;
24         case 1:week="一";break;
25         case 2:week="二";break;
26         case 3:week="三";break;
27         case 4:week="四";break;
28         case 5:week="五";break;
29         case 6:week="六";break;
30     }
31     if(hour<10){
32         hour="0"+hour;
33     }
34     if(minute<10){
35         minute="0"+minute;
36     }
37     if(second<10){
38         second="0"+second;
39     }
40     oDiv.innerHTML="今天是:"+year+"年"+month+"月"+"星期"+week+"<br>"+"当前时间:"+hour+"时"+minute+"分"+second+"秒";
41 //    setTimeout("timer()",1000);
42 }
43 setInterval("timer()",1000);
44 </script>
45 <style type="text/css">
46 #div1{
47     color:#30C;
48     background-color:#CC6;
49     display:inline-block;
50     border-radius:10px;
51     padding:10px;
52 }
53 </style>
54 </head>
55 <body>
56 <div id="div1"></div>
57 </body>
58 </html>

ps:
1.Date() 获取当前系统时间
2.getFullYear() 年
3.getMonth() 月 :取值为0-11
4.getDate() 日
5.getDay() 星期几 :取值为0-6,对应从日开始
6.getHours() 小时
7.getMinutes() 分
8.getSeconds() 秒
9.setInterval( 函数 , 时间间隔) :一直执行

posted @ 2019-09-28 12:33  New7  阅读(254)  评论(0编辑  收藏  举报