1. 程式人生 > >很實用的javascript date(時間)時針和倒計時網頁程式

很實用的javascript date(時間)時針和倒計時網頁程式

">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
        <title>Insert title here</title>
        <script type="text/javascript">
            window.onload = function(){
                var time = document.getElementById("time");
    var countDown = document.getElementById("countDown");
                //alert(time);
    
    //countTime();
    setInterval(showTime, 1);
    setInterval(countTime, 1000);
    function showTime(){
                var currentDate = new Date();
                var year = currentDate.getYear() + 1900;
                var month = currentDate.getMonth();
                var date = currentDate.getDate();
                var hour = currentDate.getHours();
                var minute = currentDate.getMinutes();
                var second = currentDate.getSeconds();
                var milis = currentDate.getMilliseconds();
    
                var day = currentDate.getDay();
                var arr = ["日","一","二","三","四","五","六"];
                time.value = "當前的時間是: " + year + "年" + month + "月" + date + "日"
                                 + (hour < 10 ? "0" + hour : hour) + "時"
            + (minute < 10 ? "0" + minute : minute) + "分"
            + (second < 10 ? "0" + second : second) + "秒"
            + (milis < 100 ? (milis < 10 ? "00" + milis : "0" + milis) : + milis ) + "毫秒 星期:"
             + arr[day];
                }
    
     //countTime();
    function countTime(){
     var target = new Date(2012,2,21,17).getTime();
     var current = new Date().getTime();
     //alert(target);
     var gap = target - current;
     var ms = gap % 1000; //不足1秒的毫秒數
     gap = Math.floor(gap / 1000); //還差多少秒
     var second = gap % 60; //不足1分鐘的秒數
     gap = Math.floor(gap / 60); //還差多少分鐘
     var minute = gap % 60; //不足1小時的分鐘數
     gap = Math.floor(gap / 60); //還差多少小時
     var hour = gap % 24;  //不足一天的小時數
     var date=  Math.floor(gap / 24); //還差幾天
     countDown.value = "距離基礎班畢業還有"+date+"天"+hour+"小時"+minute+"分鐘"+second+"秒";
     
    }
            }
        </script>
    </head>
    <body>
        <input id="time" style="border:0px; font-size:20px; color:red; width:600px"><br>
        <input id="countDown" style="border:0px; font-size:20px; color:blue; width:600px">
    </body>
</html>