<!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=utf-8" /> <title>无标题文档</title> <script type="text/javascript"> function init(){ var dax=['日','一','二','三','四','五','六'] var a=new Date(); var year=a.getFullYear(); var month=format(a.getMonth()+1); var day=format(a.getDate()); var hour=format(a.getHours()); var minute=format(a.getMinutes()); var second=format(a.getSeconds()); var da=a.getDay(); var x=year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second+" 星期"+dax[da]; document.getElementById("test").innerHTML=x; window.setTimeout(init,1000); } window.onload=init; function format(x){ return x>9?x:'0'+x; } </script> </head><body> <div id="test"></div> </body> </html> 大体这样试试
<html> <head> <script type="text/javascript"> function startTime() { var today=new Date() var h=today.getHours() var m=today.getMinutes() var s=today.getSeconds() // add a zero in front of numbers<10 m=checkTime(m) s=checkTime(s) document.getElementById('txt').innerHTML=h+":"+m+":"+s t=setTimeout('startTime()',500) }function checkTime(i) { if (i<10) {i="0" + i} return i } </script> </head><body onload="startTime()"> <div id="txt"></div> </body> </html>
最好的话还是自己写个算法,尽量避免重复创建Date对象每秒钟new一个Date对象还是不能接受..
真没有关系 这个date对象 占的空间 应该就是一个 时间刻度的数字 这就是对象的优势
<html> <head> <script type="text/javascript"> function startTime() { var today=new Date() var h=today.getHours() var m=today.getMinutes() var s=today.getSeconds() // add a zero in front of numbers<10 m=checkTime(m) s=checkTime(s) document.getElementById('txt').innerHTML=h+":"+m+":"+s t=setTimeout('startTime()',500) }function checkTime(i) { if (i<10) {i="0" + i} return i } </script> </head><body onload="startTime()"> <div id="txt"></div> </body> </html>
或Js setInterval
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function init(){
var dax=['日','一','二','三','四','五','六']
var a=new Date();
var year=a.getFullYear();
var month=format(a.getMonth()+1);
var day=format(a.getDate());
var hour=format(a.getHours());
var minute=format(a.getMinutes());
var second=format(a.getSeconds());
var da=a.getDay();
var x=year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second+" 星期"+dax[da];
document.getElementById("test").innerHTML=x;
window.setTimeout(init,1000);
}
window.onload=init;
function format(x){
return x>9?x:'0'+x;
}
</script>
</head><body>
<div id="test"></div>
</body>
</html>
大体这样试试
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()',500)
}function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</script>
</head><body onload="startTime()">
<div id="txt"></div>
</body>
</html>
真没有关系 这个date对象 占的空间 应该就是一个 时间刻度的数字
这就是对象的优势
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()',500)
}function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</script>
</head><body onload="startTime()">
<div id="txt"></div>
</body>
</html>