<script type="text/javascript"> window.onload = function() { var show = document.getElementById("show"); setInterval(function() { var time = new Date(); // 程序计时的月从0开始取值后+1 var m = time.getMonth() + 1; var t = time.getFullYear() + "-" + m + "-" + time.getDate() + " " + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds(); show.innerHTML = t; }, 1000); };</script>
html部分如下:
<div id="show"></div>
第二种
<!DOCTYPE html><html lang="en"><head><meta charset="gb2312"><title></title><script type="text/javascript"> window.onload=function(){ setInterval(function(){ var date=new Date(); var year=date.getFullYear(); //获取当前年份 var mon=date.getMonth()+1; //获取当前月份 var da=date.getDate(); //获取当前日 var day=date.getDay(); //获取当前星期几 var h=date.getHours(); //获取小时 var m=date.getMinutes(); //获取分钟 var s=date.getSeconds(); //获取秒 var d=document.getElementById('Date'); d.innerHTML='当前时间:'+year+'年'+mon+'月'+da+'日'+'星期'+day+' '+h+':'+m+':'+s; },1000) }</script></head><body><div id="Date"> </div></body></html>