js實現倒計時及時間對象


JS實現倒計時效果代碼如下:

 1 <!doctype html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>無標題文檔</title>h
 6 <style>
 7 #box {
 8     width: 100%;
 9     height: 400px;
10     background: black;
11     color: #fff;
12     font-size:40px;
13     line-height:400px;
14     text-align:center;
15 }
16 </style>
17 <script>
18 window.onload = function(){
19     var oBox = document.getElementById('box');
20     var oDate = new Date();//獲取當前時間;
21     oDate.setFullYear(2016,11,31);//自動進位;
22     oDate.setHours(0,0,0,0);
23 
24     function countDown(){
25         //未來時間戳減去現在時間的時間戳;
26         var ms = oDate.getTime() - new Date().getTime();
27 
28         //毫秒轉換成秒
29         var oSec = parseInt(ms/1000);
30 
31         //秒轉換成天
32         var oDay = parseInt(oSec/86400);
33 
34         //不到一天剩下的秒數;
35         oSec%=86400;
36 
37         //轉換成小時
38         var oHour = parseInt(oSec/3600);
39 
40         //不到一小時剩下的秒數;
41         oSec%=3600;
42 
43         //轉換成分鍾
44         var oMin = parseInt(oSec/60);
45 
46         //不到一分鍾剩下的秒數;
47         oSec%=60;
48 
49         oBox.innerHTML = '距離2016年12月31日還有:'+oDay+'天'+oHour+'時'+oMin+'分'+oSec+'秒';
50     }    
51     countDown();
52     setInterval(countDown,1000);
53 }
54 </script>
55 </head>
56 
57 <body>
58 <div id="box">距離2016年12月31日還有:xx天xx時xx分xx秒</div>
59 </body>
60 </html>

實現效果入下:

時間戳:1970年1月日至今的毫秒數:oDate.getTime(); //不要問我為什么是1970年1月至今哦!自個兒百度啦!
時間對象:
 獲取時間:
var oDate = new Date();
oYear = oDate.getFullYear();
oMon = oDate.getMonth();
oDay = oDate.getDate();
oHou = oDate.getHours();
oMin = oDate.getMinutes();
oSec = oDate.getSeconds();
oWeek = oDate.getDay();

設置時間:
oDate.setFullYear(年,月,日);
oDate.setMonth(月);
oDate.setDate(日);
oDate.setHours(時,分,秒,毫秒);
時間會自動進位;

大概整理的就這些,還有很多不足的地方,希望大家多提寶貴意見!互相學習!互相取經!~ ~ ^_^


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM