利用javascript的date获取当前时间,本人认为只需注意两点就是使用date和settimeout、这两个方法掌握的话,网页显示时间就是小问题!!
源代码:
<html>
<head>
<title>显示时间</title>
<style type="text/css">
bosy{
text-align : center ;
margin :auto ;}
#time{
width : 280px ;
height : 100px ;
background : #ccc ;
text-align : center ;
margin : auto ;
padding-top : 40px ;
}
</style>
<script type="text/javascript">
function time(){
var date = new Date();
var strY = date.getFullYear();
var strM = (date.getMonth() + 1);
var strD = date.getDate();
var strH = date.getHours();
var strMin = date.getMinutes();
var strS = date.getSeconds();
if(strM < 10){
strM = '0' + strM;
}
if(strD < 10){
strD = '0' + strD;
}
if(strH < 10){
strH = '0' + strH;
}
if(strMin < 10){
strMin = '0' + strMin;
}
if(strS < 10){
strS = '0' + strS;
}
var Time = strY + "年" + strM + "月" + strD + "日" + "现在时间:" + strH + ":" + strMin + ":" + strS;
document.getElementById("time").innerText = Time;
window.setTimeout("time()", 1000);
}
</script>
</head>
<body οnlοad="time()">
<div id="time">
</div>
</body>
</html>