Home > Web Front-end > JS Tutorial > Dynamically display the current system time based on javascript_javascript skills

Dynamically display the current system time based on javascript_javascript skills

WBOY
Release: 2016-05-16 15:17:19
Original
1897 people have browsed it

The example in this article explains the detailed code of javascript to dynamically display the current system time. The specific content is as follows

  • (1) Time and date information should be displayed in a
  • (2) Timer: Access the system time again every second, setTimeout() of the window object
  • (3) Timing of clock display (event): It is displayed after the web page is loaded, event onload
  • (4) How to write time and date information into the specified
    , the innerHTML attribute in the DOM object

Rendering:

Specific code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">


//定义函数:构建要显示的时间日期字符串
function showTime()
{
 //创建Date对象
 var today = new Date();
 //分别取出年、月、日、时、分、秒
 var year = today.getFullYear();
 var month = today.getMonth()+1;
 var day = today.getDate();
 var hours = today.getHours();
 var minutes = today.getMinutes();
 var seconds = today.getSeconds();
 //如果是单个数,则前面补0
 month  = month<10  &#63; "0"+month : month;
 day  = day <10  &#63; "0"+day : day;
 hours  = hours<10  &#63; "0"+hours : hours;
 minutes = minutes<10 &#63; "0"+minutes : minutes;
 seconds = seconds<10 &#63; "0"+seconds : seconds;
 
 //构建要输出的字符串
 var str = year+"年"+month+"月"+day+"日 "+hours+":"+minutes+":"+seconds;
 
 //获取id=result的对象
 var obj = document.getElementById("result");
 //将str的内容写入到id=result的<div>中去
 obj.innerHTML = str;
 //延时器
 window.setTimeout("showTime()",1000);
}
</script>
<style type="text/css">
#result{
 width:500px;
 border:1px solid #CCCCCC;
 background:#FFFFCC;
 margin:50px auto;
 font-size:24px;
 color:#FF0000;
 padding:20px;
}
</style>
</head>

<body onload="showTime()">
<div id="result"></div>
</body>
</html>
Copy after login

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

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