显示格式:“年-月-日 周 时:分:秒”
<script type="text/javascript">
function current_time(){
var date = new Date();
var year = date.getFullYear();
//getMonth()从0到11
var month = date.getMonth()+1;
month = month<10?'0'+month:month;
//getdate()从1到31
var day = date.getDate();
//使其始终显示两位数,小于10的时候在前面填充0,下面类似
day = day<10?'0'+day:day;
//用三目运算符直接将0-6转换为标准的“星期#”
var week = '星期' + '日一二三四五六'.charAt(date.getDay());
var hour = date.getHours();
hour = hour<10?'0'+hour:hour;
var minute = date.getMinutes();
minute = minute<10?'0'+minute:minute;
var second = date.getSeconds();
second = second<10?'0'+second:second;
//拼接为“年-月-日 周 时:分:秒”格式
var time = year+'-'+month+'-'+day+' '+week+' '+hour+':'+minute+':'+second;
document.getElementById('time').innerHTML = time;
}
//在加载页面的时候直接执行一次,使其不至于延迟一秒才执行
current_time();
//每隔一秒执行一次,刷新页面的时间
setInterval('current_time()',1000);
</script>