date制作电子时钟

首先我们得获取时间,利用函数date来获取各个需要的值:

View Code
 1 var time = new Date();
 2     var year = time.getFullYear();
 3     var month = time.getMonth()+1;
 4     var day = time.getDate();
 5     var week = time.getDay();
 6     var hour = time.getHours();
 7     var minute  = time.getMinutes();
 8     var second  = time.getSeconds();
 9     var flag = 'AM';
10     switch(week){
11         case 1:week = "一"; break;
12         case 2:week = "二"; break;
13         case 3:week = "三"; break;
14         case 4:week = "四"; break;
15         case 5:week = "五"; break;
16         case 6:week = "六"; break;
17         default: week = "日";break;
18     }
19     if(hour<10){
20         hour = "0" + hour;
21         
22     }
23     if(hour>12){
24         flag = "PM";
25     }
26     if(minute<10){
27         minute = "0" + minute;
28     }
29     if(second<10){
30         second = "0" + second;
31     }
32     var result = year+"年 "+month+"月 "+day+"日  星期"+week+" "+hour+":"+minute+":"+second + " "+flag;
33     document.write(result);


如果要想让它自动刷新,就看制作电子表(二)吧。。

posted @ 2012-08-04 15:29  妍珊  阅读(383)  评论(0编辑  收藏  举报