首页 > 代码库 > JS即时显示当前时间

JS即时显示当前时间

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>当前系统时间</title>
<link rel="stylesheet" href="style.css"  />
<script language="javascript" type="text/javascript">

  window.onload = function(){
    setInterval(showTime(),500);
  }

  function checkTime(i){  //补位处理
    i = i<10?0+i:i;
    return i;
  }

  function showTime(){
    var now=new Date();
    console.log(now);
    var year=now.getFullYear();
    var month=now.getMonth()+1;
    var day=now.getDate();
    var h=now.getHours();
    var m=now.getMinutes();
    var s=now.getSeconds();
    m=checkTime(m);
    s=checkTime(s);
    var week = now.getDay();
    var weekday=new Array(7);
    weekday[0]="星期日";
    weekday[1]="星期一";
    weekday[2]="星期二";
    weekday[3]="星期三";
    weekday[4]="星期四";
    weekday[5]="星期五";
    weekday[6]="星期六";

    document.getElementById("show").innerHTML=""+year+""+month+""+day+""+weekday[week]+h+":"+m+":"+s;
  }

</script>
</head>
<body>
<div class="content1">
  <div id="show">显示时间的位置</div>
</div>
</body>
</html>

遇到的问题及注意事项:

1、创建时间new Date(2017,6,6);和new Date("2017,6,6");两个结果不一样,加双引号就是6月,不加就是7月。

JS即时显示当前时间