JS实现以日历形式显示当前时间

效果图:

效果图

 

1 <script language="Javascript"> 
2 var datelocalweek=new Array("星期日", "星期一", "星期二","星期三","星期四", "星期五","星期六"); 
3 var datelocalnow=new Date(); 
4 var datelocalyear=datelocalnow.getFullYear(); 
5 var datelocalmonth=(datelocalmonth="0"+(datelocalnow.getMonth()+1)).substr(datelocalmonth.length-2,2); 
6 var datelocalday=(datelocalday="0"+datelocalnow.getDate()).substr(datelocalday.length-2,2); 
7 var datelocalweekday=datelocalweek[datelocalnow.getDay()]; 
8 document.write(datelocalyear+"年"+datelocalmonth+"月"+datelocalday+"日"+" "+datelocalweekday); 
9 </script> 
View Code
  1 //创建一个数组,用于存放每个月的天数 
  2 function montharr(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11) { 
  3 this[0] = m0; 
  4 this[1] = m1; 
  5 this[2] = m2; 
  6 this[3] = m3; 
  7 this[4] = m4; 
  8 this[5] = m5; 
  9 this[6] = m6; 
 10 this[7] = m7; 
 11 this[8] = m8; 
 12 this[9] = m9; 
 13 this[10] = m10; 
 14 this[11] = m11; 
 15 } 
 16 //实现月历 
 17 function calendar() { 
 18 var monthNames = "JanFebMarAprMayJunJulAugSepOctNovDec"; 
 19 var today = new Date(); 
 20 var thisDay; 
 21 var monthDays = new montharr(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
 22 year = today.getYear() +1900; 
 23 thisDay = today.getDate(); 
 24 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) 
 25 monthDays[1] = 29; 
 26 nDays = monthDays[today.getMonth()]; 
 27 firstDay = today; 
 28 firstDay.setDate(1); 
 29 testMe = firstDay.getDate(); 
 30 if (testMe == 2) 
 31 firstDay.setDate(0); 
 32 startDay = firstDay.getDay(); 
 33 var dayNames = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); 
 34 var monthNames = new Array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"); 
 35 var now = new Date(); 
 36 document.write("<div id='rili' style='position:absolute;width:140px;left:200px;top:50px;'>") 
 37 document.write("<TABLE width='217' BORDER='0' CELLSPACING='0' CELLPADDING='2' BGCOLOR='#0080FF'>") 
 38 document.write("<TR>"); 
 39 document.write(" <TD> "); 
 40 document.write(" <table border='0' cellspacing='1' cellpadding='2' bgcolor='Silver'>"); 
 41 document.write(" <TR><th colspan='7' bgcolor='#C8E3FF'>"); 
 42 document.writeln(" <FONT STYLE='font-size:9pt;Color:#330099'>" + "公元 " + now.getYear() + "年" + monthNames[now.getMonth()] + " " + now.getDate() + "日 " + dayNames[now.getDay()] + "</FONT>"); 
 43 document.writeln(" </TH></TR><TR><TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>日</FONT></TH>"); 
 44 document.writeln(" <th bgcolor='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>一</FONT></TH>"); 
 45 document.writeln(" <TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>二</FONT></TH>"); 
 46 document.writeln(" <TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>三</FONT></TH>"); 
 47 document.writeln(" <TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>四</FONT></TH>"); 
 48 document.writeln(" <TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>五</FONT></TH>"); 
 49 document.writeln(" <TH BGCOLOR='#0080FF'><FONT STYLE='font-size:9pt;Color:White'>六</FONT></TH>"); 
 50 document.writeln(" </TR><TR>"); 
 51 column = 0; 
 52 for (i=0; i<startDay; i++) { 
 53 document.writeln("\n<TD><FONT STYLE='font-size:9pt'> </FONT></TD>"); 
 54 column++; 
 55 } 
 56 
 57 for (i=1; i<=nDays; i++) { 
 58 if (i == thisDay) { 
 59 document.writeln("</TD><TD ALIGN='CENTER' BGCOLOR='#FF8040'><FONT STYLE='font-size:9pt;Color:#ffffff'><B>") 
 60 } 
 61 else { 
 62 document.writeln("</TD><TD BGCOLOR='#FFFFFF' ALIGN='CENTER'><FONT STYLE='font-size:9pt;font-family:Arial;font-weight:bold;Color:#330066'>"); 
 63 } 
 64 document.writeln(i); 
 65 if (i == thisDay) 
 66 document.writeln("</FONT></TD>") 
 67 column++; 
 68 if (column == 7) { 
 69 document.writeln("<TR>"); 
 70 column = 0; 
 71 } 
 72 } 
 73 document.writeln("<TR><TD COLSPAN='7' ALIGN='CENTER' VALIGN='TOP' BGCOLOR='#0080FF'>") 
 74 document.writeln("<FORM NAME='clock' onSubmit='0'><FONT STYLE='font-size:9pt;Color:#ffffff'>") 
 75 document.writeln("现在时间:<INPUT TYPE='Text' NAME='face' ALIGN='TOP'></FONT></FORM></TD></TR></TABLE>") 
 76 document.writeln("</TD></TR></TABLE></div>"); 
 77 } 
 78 
 79 var timerID = null; 
 80 var timerRunning = false; 
 81 
 82 function stopclock (){ 
 83 if(timerRunning) 
 84 clearTimeout(timerID); 
 85 timerRunning = false; 
 86 } 
 87 
 88 //显示当前时间 
 89 function showtime () { 
 90 var now = new Date(); 
 91 var hours = now.getHours(); 
 92 var minutes = now.getMinutes(); 
 93 var seconds = now.getSeconds() 
 94 var timeValue = " " + ((hours >12) ? hours -12 :hours) 
 95 timeValue += ((minutes < 10) ? ":0" : ":") + minutes 
 96 timeValue += ((seconds < 10) ? ":0" : ":") + seconds 
 97 timeValue += (hours >= 12) ? " 下午 " : " 上午 " 
 98 document.clock.face.value = timeValue; 
 99 timerID = setTimeout("showtime()",1000);//设置超时,使时间动态显示 
100 timerRunning = true; 
101 } 
102 
103 function startclock () { 
104 stopclock(); 
105 showtime(); 
106 } 
Show Time
1 <html> 
2 <script language="JavaScript" type="text/javascript" src="time.js"> </script> 
3 <body bgcolor="#FFFFFF" onLoad="startclock(); timerONE=window.setTimeout" text="#000000"> 
4 <script language="JavaScript" type="text/javascript"> 
5 calendar(); 
6 </script> 
7 </body> 
8 </html> 

 

posted @ 2013-07-24 10:19  殷跃杰  阅读(851)  评论(1编辑  收藏  举报