使用js实现时间显示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>时间</title>
<style type="text/css">
#main{
margin:20px 100px;
height:31px;
}
#title
{
font:Arial, Helvetica, sans-serif;
font-size:12px;
color:#0066FF;
float:right;
margin-right:10px;
}
#txt
{
width:200px;
height:20px;
border:1px  ;
float:right;

 
}
#info
{
display:block
width:100%;
height:30px;
background-color:#999999;
}

</style>
<script type="text/javascript">
function CurrentTime()
{
 var str="";
 //创建时间对象
 var time=new Date();
 var year=time.getYear();
 var month=time.getMonth()+1;
 
 var day=time.getDate();
 var hour=time.getHours();
 var minute=time.getMinutes();
 var second=time.getSeconds();
 var day=time.getDay();
 str=year+"年"+month+"月"+day+"日"+"   "+hour+":"+minute+":"+second+"  "+"星期"+day;
 
 document.getElementById("txt").innerHTML="<font size='2' color='red'>"+str+"</font>";
}
function Init()
{
 setInterval("CurrentTime()",1000);
}
</script>
</head>

<body onload="Init()">
<div id="main">
 <div id="info">
  
  <div id="txt">
  
  </div>
  <span id="title">当前时间:</span>
 </div>
</div>
</body>
</html>

需要注意的是:getDay()是星期的数字表示,getMonth()获取的月份从0开始。
posted @ 2009-10-27 10:55  坐看风起  阅读(1558)  评论(0编辑  收藏  举报