javascript實現即時輸出當前的時間

來源:互聯網
上載者:User

   在網頁中即時的顯示時間,不但可以給網頁添色,還可以方便瀏覽者掌握目前時間,為了提高網站的開發速度,可以把主代碼封裝在一個單獨的函數裡面,在需要的時候直接調用而我為了示範,直接寫在了首頁面,方便大家觀看

  最近有個網站頁面上要輸出目前時間,精確到時分秒,還要適時變化的。百度找了找,沒找到合適的,於是自己寫了個,收藏下,方便以後使用。

  js 適時輸出當前的時間

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 function CurentTime(divID){ var curTime=new Array(); var now=new Date(); var week=['日','一','二','三','四','五','六']; var year=now.getFullYear(); //年 var month=now.getMonth()+1; //月 var day=now.getDate(); //日 var hh=now.getHours(); //時 var mm=now.getMinutes(); //分 var sc=now.getSeconds(); //秒 var wk=now.getDay(); //周 curTime['year']=year; curTime['month']=month<10?'0'+month:month; curTime['day']=day<10?'0'+day:day; curTime['hh']=hh<10?'0'+hh:hh; curTime['mm']=mm<10?'0'+mm:mm; curTime['sc']=sc<10?'0'+sc:sc; curTime['wk']='星期'+week[wk]; curTime=curTime['year']+'年'+curTime['month']+'月'+curTime['day']+'日'+' '+curTime['wk']+' '+curTime['hh']+':'+curTime['mm']+':'+curTime['sc']; document.getElementById(divID).innerHTML='今天是:'+curTime; setTimeout('CurentTime(''+divID+'')',1000); }

  使用方法:

  假設頁面上存在一個id命名為time的div,則:

  ?

1 <script language="javascript">CurentTime('time');</script>

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

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