javascript學習筆記(三)顯示當時時間的代碼

來源:互聯網
上載者:User

html代碼如下: 複製代碼 代碼如下:<div id="showTime"></div>

javascript 代碼如下:時間顯示格式為:2011-04-04 星期二 12:09:34 複製代碼 代碼如下:function startTime() {
try{
var today = new Date();
var year = today.getFullYear();
var month = checkNum(today.getMonth()+1);
var date = checkNum(today.getDate());
var day = today.getDay();
var hour = checkNum(today.getHours());
var minute = checkNum(today.getMinutes());
var second = checkNum(today.getSeconds());
switch(day) {
case 1:
day = "一";
break;
case 2:
day = "二";
break;
case 3:
day = "三";
break;
case 4:
day = "四";
break;
case 5:
day = "五";
break;
case 6:
day = "六";
break;
case 7:
day = "日";
break;
}
var fullTime = year+"-"+month+"-"+date+" "+"星期"+day+" "+hour+":"+minute+":"+second;
document.getElementById("showTime").innerHTML = fullTime;
setInterval('startTime()', 1000);
}catch(e) {
alert(e.description);
}
}
function checkNum(num) {
if(num<10) {
num = "0"+num;
}
return num;
}

相關文章

聯繫我們

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