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

來源:互聯網
上載者:User

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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>

 

摘自  代俊建的專欄 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.