Home  >  Article  >  Web Front-end  >  How to obtain JS dynamic date and time_javascript skills

How to obtain JS dynamic date and time_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:37:441074browse

Since the code is relatively simple, the editor will not explain it anymore. You will understand it by looking at the code and post the example directly.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>实时时间</title>
  <script type="text/javascript">
    var slidint;
    function newTimer() {
      stopit();
      var today;
      today = new Date();
      var str = today.toLocaleDateString();
      str += "  " + week();
      str += "  " + today.toLocaleTimeString();
      var o = document.getElementById("lblDateTime");
      o.innerHTML = str;
      slidint = setTimeout(newTimer, 1000);
    }
      
    function stopit() {
      clearTimeout(slidint);
    }
      
    function week() {
      var d, day, x, s = " ";
      var x = new Array("星期日", "星期一", "星期二");
      var x = x.concat("星期三", "星期四", "星期五");
      var x = x.concat("星期六");
      d = new Date();
      day = d.getDay();
      return (s += x[day]);
    }
      
    window.onload = function() {
      newTimer();
    }
  </script>
</head>
<body>
  <label id="lblDateTime"></label>
</body>
</html>

The code implementation effect is as follows:

There are many ways to obtain dynamic time in js, and everyone needs to summarize them. I hope this article will be helpful to everyone's javascript programming design.

Statement:
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