js擷取當前日期時間同時顯示星期

來源:互聯網
上載者:User

標籤:

JavaScript擷取當前日期時間同時顯示星期幾,具體代碼如下:

  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  4. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> 
  5. <script type="text/javascript"> 
  6. function currentTime(){ 
  7.     var d=new Date(),str=‘‘; 
  8.     str+=d.getFullYear()+‘年‘; 
  9.     str+=d.getMonth() + 1+‘月‘; 
  10.     str+=d.getDate()+‘日‘; 
  11.     str+=d.getHours()+‘時‘; 
  12.     str+=d.getMinutes()+‘分‘; 
  13.     str+= d.getSeconds()+‘秒‘; 
  14.     return str; 
  15. } 
  16. setInterval(function(){$(‘#time‘).html(currentTime)},1000); 
  17. </script> 
  18. </head> 
  19. <body> 
  20. <div id="time"></div> 
  21. </body> 
  22. </html> 

在網頁上及時動態顯示當前的日期時間並顯示星期的做法:

function showTime() {
var show_day = new Array(‘星期一‘, ‘星期二‘, ‘星期三‘, ‘星期四‘, ‘星期五‘, ‘星期六‘, ‘星期日‘);
var time = new Date();
var year = time.getFullYear();
var month = time.getMonth() + 1;
var date = time.getDate();
var day = time.getDay();
var hour = time.getHours();
var minutes = time.getMinutes();
var second = time.getSeconds();
month < 10 ? month = ‘0‘ + month : month;
//month = month + 1;
//alert(year);
hour < 10 ? hour = ‘0‘ + hour : hour;
minutes < 10 ? minutes = ‘0‘ + minutes : minutes;
second < 10 ? second = ‘0‘ + second : second;
var now_time = year + ‘年‘ + month + ‘月‘ + date + ‘日‘ + ‘ ‘ + show_day[day - 1] + ‘ ‘ + hour + ‘:‘ + minutes + ‘:‘ + second;
document.getElementById(‘lishow‘).innerHTML = now_time;
//setTimeout("showTime();", 1000);
}
setInterval("showTime();", 1000);

js擷取當前日期時間同時顯示星期

聯繫我們

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