javascript学习笔记(三)显示当时时间的代码

时间:2021-05-26

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;
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章