Home > Web Front-end > JS Tutorial > body text

js method to obtain and display the current time in real time_javascript skills

WBOY
Release: 2016-05-16 15:47:57
Original
1052 people have browsed it

The example in this article describes how js can obtain and display the current time in real time. Share it with everyone for your reference. The specific implementation method is as follows:

The js part is as follows:

<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>
Copy after login

The html part is as follows:

Copy code The code is as follows:

The displayed value is as follows:

2015-7-31 8:47:5

I hope this article will be helpful to everyone’s JavaScript programming design.

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!