获取系统时间是Javascript常用模块,做了个比较完整清晰的模块,在此备忘
<script type="text/javascript">
function startTime()
{
var today=new Date()
var y=today.getFullYear()
var mon=today.getMonth()+1
var d=today.getDate()
var hour=today.getHours()
var min=today.getMinutes()
var sec=today.getSeconds()
var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
var w= weeks[today.getDay()]
min=checkTime(min)
sec=checkTime(sec)
document.getElementById('txt').innerHTML=y+" 年 "+mon+" 月 "+d+" 日 "+w+" "+hour+":"+min+":"+sec
t=setTimeout('startTime()',500)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</script>
全部
<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date()
var y=today.getFullYear()
var mon=today.getMonth()+1
var d=today.getDate()
var hour=today.getHours()
var min=today.getMinutes()
var sec=today.getSeconds()
var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
var w= weeks[today.getDay()]
min=checkTime(min)
sec=checkTime(sec)
document.getElementById('txt').innerHTML=y+" 年 "+mon+" 月 "+d+" 日 "+w+" "+hour+":"+min+":"+sec
t=setTimeout('startTime()',500)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</script>
</head>
<body onl oad="startTime()">
<div id="txt"></div>
</body>
</html>
效果