很实用的javascript date(时间)时针和倒计时网页程序

来源:岁月联盟 编辑:exp 时间:2012-03-20

<!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>

 

摘自  代俊建的专栏