js中利用定时器每隔1000毫秒显示一次(1000毫秒=1秒):
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 <style type="text/css"> 7 body{ font-size:30px;} 8 </style> 9 <script type="text/javascript"> 10 window.οnlοad=function(){ 11 showTime(); 12 }; 13 function showTime(){ 14 var myDate=new Date(); 15 var Year=myDate.getFullYear(); 16 var Month=myDate.getMonth()+1; 17 var Day=myDate.getDate(); 18 var d=myDate.getDay(); 19 var week=['星期日','星期一','星期二','星期三','星期四','星期五','星期六']; 20 var Wek=week[d]; 21 var Hour=myDate.getHours(); 22 var Minute=myDate.getMinutes(); 23 var Second=myDate.getSeconds(); 24 Day=checkTime(Day); 25 Hour=checkTime(Hour); 26 Minute=checkTime(Minute); 27 Second=checkTime(Second); 28 document.getElementById('text').innerHTML='今天是:'+Year+'年'+Month+'月'+Day+'日'+Wek+Hour+'时'+Minute+'分'+Second+'秒'; 29 setTimeout(showTime,500) 30 } 31 //当时分秒位数小于两位时,以02格式显示 32 function checkTime(i){ 33 34 if(i<10) 35 { 36 i='0'+i; 37 } 38 return i; 39 } 40 41 </script> 42 43 </head> 44 45 <body> 46 <div id="text"></div> 47 </body> 48 49 </html>