Home >Web Front-end >JS Tutorial >js method to obtain and display the current time in real time_javascript skills

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

WBOY
WBOYOriginal
2016-05-16 15:47:571106browse

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>

The html part is as follows:

Copy code The code is as follows:
fb3f21ecc505d7107df6b2ec1e6f058116b28748ea4df4d9c2150843fecfba68

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.

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