完整显示当前日期和时间的JS代码(2007年2月25日星期日正午12:42:48)
代码演示效果为“2007年2月25日星期日正午12:42:48”。
使用方法:将下面的JS代码放到你想要显示的页面中(支持HTML页面),然后在你想要显示时间的位置插入下面的代码即可
<div
align="center" style="font-size: 12px; c
1 <script> 2 function tick() { 3 var hours, minutes, seconds, xfile; 4 var intHours, intMinutes, intSeconds; 5 var today, theday; 6 today = new Date(); 7 function initArray(){ 8 this.length=initArray.arguments.length 9 for(var i=0;i<this.length;i++) 10 this[i+1]=initArray.arguments[i] } 11 var d=new initArray( 12 "星期日", 13 "星期一", 14 "星期二", 15 "星期三", 16 "星期四", 17 "星期五", 18 "星期六"); 19 theday = today.getYear()+"年" + [today.getMonth()+1]+"月" +today.getDate() + d[today.getDay()+1]; 20 intHours = today.getHours(); 21 intMinutes = today.getMinutes(); 22 intSeconds = today.getSeconds(); 23 if (intHours == 0) { 24 hours = "12:"; 25 xfile = "午夜"; 26 } else if (intHours < 12) { 27 hours = intHours+":"; 28 xfile = "上午"; 29 } else if (intHours == 12) { 30 hours = "12:"; 31 xfile = "正午"; 32 } else { 33 intHours = intHours - 12 34 hours = intHours + ":"; 35 xfile = "下午"; 36 } 37 if (intMinutes < 10) { 38 minutes = "0"+intMinutes+":"; 39 } else { 40 minutes = intMinutes+":"; 41 } 42 if (intSeconds < 10) { 43 seconds = "0"+intSeconds+" "; 44 } else { 45 seconds = intSeconds+" "; 46 } 47 timeString = theday+xfile+hours+minutes+seconds; 48 Clock.innerHTML = timeString; 49 window.setTimeout("tick();", 100); 50 } 51 window.onload = tick; 52 </script>
olor:#000000"></div>
JS代码如下: