JS获取系统时间--JavaScript基础

1、网页中实时显示当前时间

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>页面上显示系统时间</title>
</head>
<body onload="showTime()">
  当前时间为:<span id="show"></span>
</body>
<script>
  function showTime() {
    var nowTime = new Date();
    var years = nowTime.getFullYear();
    var mouths = nowTime.getMonth();
    var days = nowTime.getDate();
    var hours = nowTime.getHours();
    var minites = nowTime.getMinutes();
    var seconds = nowTime.getSeconds();
    var time = years+"年";
    time += (mouths+1)+"月"+days+"日";
    time += (hours<10?"0":"")+hours;
    time += (minites<10?":0":":") + minites;
    time += (seconds<10?":0":":") + seconds;
    time += (hours>12)?"PM":"AM";
    document.getElementById("show").innerHTML = time;
    setTimeout("showTime()",1000);
    }
</script>
</html>

2、实现效果:

 




posted @ 2017-09-22 21:30  牛新龙的IT技术博客  阅读(567)  评论(0编辑  收藏  举报