js动态显示时间

js代码:

function getDate(){     
var date=new Date();     
var month=date.getMonth()+1;     
var day=date.getDate();     
    
if(month.toString().length == 1){  //或者用if (eval(month) <10) {month="0"+month}     
month='0'+month;     
}     
if(day.toString().length == 1){     
day='0'+day;     
}   

return date.getYear()+'/'+month+'/'+day+'  '+date.toLocaleString().substring(date.toLocaleString().length-10)+'  '+'星期'+'日一二三四五六'.charAt(date.getDay());
}

----------------------------------

Html代码:
<!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 language="javascript" src= "date.js"></script>
</head>

当前时间:<div id="time">     
<script>     
document.getElementById('time').innerHTML=getDate();     
setInterval("document.getElementById('time').innerHTML=getDate();",1000);     
</script>      
</div> 
</body>
</html>

你可能感兴趣的:(js)