萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> js獲取當前日期代碼適用於網頁頭部

js獲取當前日期代碼適用於網頁頭部

代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function bodyLoad(){
var dateTime=new Date();
var hh=dateTime.getHours();
if(hh<10) hh = '0' + hh;
var mm=dateTime.getMinutes();
if(mm<10) mm = '0' + mm;
var ss=dateTime.getSeconds();
if(ss<10) ss = '0' + ss;
var yy=dateTime.getFullYear();

var MM=dateTime.getMonth()+1; //因為1月這個方法返回為0,所以加1
var dd=dateTime.getDate();
var week=dateTime.getDay();
var days=[ "日 ", "一 ", "二 ", "三 ", "四 ", "五 ", "六 ",]
document.getElementById("date").value=yy+"年"+MM+"月"+dd+"日 "+"星期"+days[week] ;
document.getElementById("time").value=hh+":"+mm+":"+ss;
setTimeout(bodyLoad,1000);
}
</script>
</head>
<body onload="bodyLoad()">
<input type="text" id="date" style="border:0 none;" /><input type="text" id="time" style="border:0 none;" />
</body>
</html>

copyright © 萬盛學電腦網 all rights reserved