Home > Web Front-end > JS Tutorial > How to get the current system time in javascript_javascript skills

How to get the current system time in javascript_javascript skills

WBOY
Release: 2016-05-16 15:31:19
Original
1300 people have browsed it

The example in this article describes the javascript code to obtain the current time of the system. Share it with everyone for your reference. The details are as follows:
The screenshot of the running effect is as follows:

The specific code is as follows:

<!DOCTYPE html>
<html>
<head>
  <title>获取时间</title>
  <script type="text/javascript">
  window.onload = function(){
    showTime();
  }

  function showTime(){
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var date = myDate.getDate();
    var dateArr = ["日","一",'二','三','四','五','六'];
    var day = myDate.getDay();
    var hours = myDate.getHours();
    var minutes = formatTime(myDate.getMinutes());
    var seconds = formatTime(myDate.getSeconds());

    var systemTime = document.getElementById("time");
    systemTime.innerHTML = " " + year + "年" + month +"月" + date + "日" + " 星期" + dateArr[day] + " " + hours + ":" + minutes + ":" + seconds;
    setTimeout("showTime()",500);
  }

  //格式化时间:分秒。
  function formatTime (i){
    if(i < 10){
      i = "0" + i;
    }
    return i;
  }

  </script>
</head>
<body>
  <div id ='time'></div>
</body>
</html>
Copy after login

The above is the method of obtaining the current system time in JavaScript shared with you to help you better learn the Date object of JavaScript. I hope you like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template