js實現倒計時及時間對象

來源:互聯網
上載者:User

標籤:val   技術分享   pre   時間戳記   gets   對象   cond   nload   var   

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(時,分,秒,毫秒);
時間會自動進位;

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

js實現倒計時及時間對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.