new Date() 之高考倒計時

簡單瞭解下new Date()

var myDate = new Date();
myDate.getYear(); //獲取當前年份(2位)
myDate.getFullYear(); //獲取完整的年份(4位,1970-????)
myDate.getMonth(); //獲取當前月份(0-11,0代表1月)         // 所以獲取當前月份是myDate.getMonth()+1; 
myDate.getDate(); //獲取當前日(1-31)
myDate.getDay(); //獲取當前星期X(0-6,0代表星期天)
myDate.getTime(); //獲取當前時間(從1970.1.1開始的毫秒數)
myDate.getHours(); //獲取當前小時數(0-23)
myDate.getMinutes(); //獲取當前分鐘數(0-59)
myDate.getSeconds(); //獲取當前秒數(0-59)
myDate.getMilliseconds(); //獲取當前毫秒數(0-999)
myDate.toLocaleDateString(); //獲取當前日期
var mytime=myDate.toLocaleTimeString(); //獲取當前時間
myDate.toLocaleString( ); //獲取日期與時間

2017年高考倒計時:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>countdown</title>
</head>

<body>
    <p id="countdown"></p>
    <script>
    function countDown() {
        var currentDate = new Date(),
            endDate = new Date("2017/6/7,9:00"),
            leftTime = endDate - currentDate,
            leftDay = checkNum(parseInt(leftTime / (1000 * 60 * 60 * 24))),
            leftHours = checkNum(parseInt(leftTime / (1000 * 60 * 60) % 24)),
            leftMinutes = checkNum(parseInt(leftTime / (1000 * 60) % 60)),
            leftSeconds = checkNum(parseInt(leftTime / 1000 % 60)),
            countDown = document.getElementById('countdown');
            countDown.innerHTML = '高考倒計時:' + leftDay + '天' + leftHours + '小時' + leftMinutes + '分' + leftSeconds + '秒'

            // 判斷是否小於零
            function checkNum(num){
            	if(num<10){
            		num = '0' + num;
            	}
            	return num;
            }
            // 判斷是否到達節點 清除定時器
            if(leftTime<0){
            	countDown.innerHTML = '高考已經開始,等待你們的好消息!'
            	clearInterval(timer);
            }
    }
    var timer;
    timer = setInterval(countDown,500)
    </script>
</body>

</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章