js中时间的获取、定时器的使用


 

 1   <style>
 2  .time {
 3  background-color: antiquewhite;
 4  width: 150px;
 5  height: 50px;
 6  text-align: center;
 7  font-size: 20px;
 8       }
 9   </style>
10   <script type="text/javascript">
11         function getTime() { 12             var now = new Date(); 13             //获取相对于1900的时间差,比如2019年,获取的数字是 119
14             var year = now.getYear(); 15             //获取完整的年份
16  year = now.getFullYear(); 17             //范围0~11, 跟中国的月份相差1
18             var month = now.getMonth()+1; 19             //getDate表示获取当前时间对应的月份中的天数
20             var days = now.getDate(); 21             //表示获取星期数 ,西方国家的星期数是 0~6,0代表的是周日,其他的分别是周一到周六
22             var dayOfWeek = now.getDay() ; 23             //获取当前小时
24             var hour = now.getHours(); 25             //获取当前分钟
26             var minutes = now.getMinutes(); 27             //获取当前秒
28             var seconds = now.getSeconds(); 29             //拼接时间格式, 年-月-日 时:分:秒
30             var time1 = year+"-"+month+"-"+days+"  "+hour+":"+minutes+":"+seconds; 31             
32  document.getElementById("time").innerText = time1; 33  } 34         //定义将使用的定时器
35         var timeout = setInterval(); 36         
37         //打开定时器
38         function startInterval() {
39 timeout = setInterval("getTime()",1000); 40 }              //setInterval(调用方法,刷新间隔) 41 42 //清空定时器,即停止定时器 43 function closeInterval() { 44 clearInterval(timeout); 45 }         46 47 </script> 48 </head> 49 50 <body background="../../img/imgBg/blueBg2.jpg"> 51 52 <div id="time" class="time">显示时间</div><br /> 53 54 <input type="button" name="closeBtn" value="停止" onclick="closeInterval()" /> 55 56 <input type="button" name="startBtn" value="开始" onclick="startInterval()" /> 57 58 </body> 59

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM