javascript簡單擷取當前日期和時間

來源:互聯網
上載者:User
javascript簡單擷取當前日期和時間(www.dayday28.com)

有時候可能會有這樣的需求,方便使用者看時間看日期。以前也不動手寫都是偷,今天無聊就自己也寫了個,挺簡單的。下面是,時間會每秒計算一次。Demo

下面是Html code:

<!DOCTYPE html><html class="no-js"><head>    <meta charset="UTF-8">    <title>當期時間</title>    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>    <style type="text/css">        .date{height:100px;line-height:100px;margin:0 auto;font-family:Arial, Helvetica, sans-serif;font-size:40px;        border:solid 1px #ccc;box-shadow:0 0 3px rgba(0,0,0,0.3) inset;border-radius:5px;text-shadow:1px 1px 2px rgba(0,0,0,0.3);}        .date strong{padding:0 10px;display:inline-block;height:100px;border-right:solid 1px #ccc;}    </style></head><body>    <h1>js簡單的擷取當前日期和時間</h1>    <p class="date">        <strong id="Y"></strong>        <strong id="MH"></strong>        <strong id="TD"></strong>        <strong id="D"></strong>        <strong id="H"></strong>        <strong id="M"></strong>        <strong id="S"></strong>     </p>       <script type="text/javascript">                $(document).ready(function(){                    setInterval(showTime, 1000);                                        function timer(obj,txt){                            obj.text(txt);                    }                       function showTime(){                                        var today = new Date();                        var weekday=new Array(7)                        weekday[0]="星期一"                        weekday[1]="星期二"                        weekday[2]="星期三"                        weekday[3]="星期四"                        weekday[4]="星期五"                        weekday[5]="星期六"                        weekday[6]="星期日"                                            var y=today.getFullYear()+"年";                        var month=today.getMonth()+"月";                        var td=today.getDate()+"日";                        var d=weekday[today.getDay()];                        var h=today.getHours();                        var m=today.getMinutes();                        var s=today.getSeconds();                           timer($("#Y"),y);                        timer($("#MH"),month);                          timer($("#TD"),td);                         timer($("#D"),d);                        timer($("#H"),h);                        timer($("#M"),m);                        timer($("#S"),s);                    }                   })    </script>     </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.