首頁  >  文章  >  web前端  >  完整显示当前日期和时间的JS代码_时间日期

完整显示当前日期和时间的JS代码_时间日期

WBOY
WBOY原創
2016-05-16 19:09:031439瀏覽

代码演示效果为“2007年2月25日星期日正午12:42:48”。
  使用方法:将下面的JS代码放到你想要显示的页面中(支持HTML页面),然后在你想要显示时间的位置插入下面的代码即可



  JS代码如下:

<script> <BR>function tick() { <BR>var hours, minutes, seconds, xfile; <BR>var intHours, intMinutes, intSeconds; <BR>var today, theday; <BR>today = new Date(); <BR>function initArray(){ <BR>this.length=initArray.arguments.length <BR>for(var i=0;i<this.length;i++) <BR>this[i+1]=initArray.arguments[i] } <BR>var d=new initArray( <BR>"星期日", <BR>"星期一", <BR>"星期二", <BR>"星期三", <BR>"星期四", <BR>"星期五", <BR>"星期六"); <BR>theday = today.getYear()+"年" + [today.getMonth()+1]+"月" +today.getDate() + d[today.getDay()+1]; <BR>intHours = today.getHours(); <BR>intMinutes = today.getMinutes(); <BR>intSeconds = today.getSeconds(); <BR>if (intHours == 0) { <BR>hours = "12:"; <BR>xfile = "午夜"; <BR>} else if (intHours < 12) { <BR>hours = intHours+":"; <BR>xfile = "上午"; <BR>} else if (intHours == 12) { <BR>hours = "12:"; <BR>xfile = "正午"; <BR>} else { <BR>intHours = intHours - 12 <BR>hours = intHours + ":"; <BR>xfile = "下午"; <BR>} <BR>if (intMinutes < 10) { <BR>minutes = "0"+intMinutes+":"; <BR>} else { <BR>minutes = intMinutes+":"; <BR>} <BR>if (intSeconds < 10) { <BR>seconds = "0"+intSeconds+" "; <BR>} else { <BR>seconds = intSeconds+" "; <BR>} <BR>timeString = theday+xfile+hours+minutes+seconds; <BR>Clock.innerHTML = timeString; <BR>window.setTimeout("tick();", 100); <BR>} <BR>window.onload = tick; <BR></script>


  比较实用的代码,值得珍藏^_^
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn