javascript 当前页面实时显示当前时间的简单示例

前端之家收集整理的这篇文章主要介绍了javascript 当前页面实时显示当前时间的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
JavaScript实时显示当前时间的例子,经测试代码如下:

<html>
<head> 
<title>JavaScript 实时显示当前时间</title> 
</head> 
<body> 
<script language="javascript"> 
/**
 * 实时显示当前时间
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
function showtime() 
{ 
var today,hour,second,minute,year,month,date; 
var strDate ; 
today=new Date(); 
var n_day = today.getDay(); 
switch (n_day) 
{ 
case 0:{ 
strDate = "星期日" 
}break; 
case 1:{ 
strDate = "星期一" 
}break; 
case 2:{ 
strDate ="星期二" 
}break; 
case 3:{ 
strDate = "星期三" 
}break; 
case 4:{ 
strDate = "星期四" 
}break; 
case 5:{ 
strDate = "星期五" 
}break; 
case 6:{ 
strDate = "星期六" 
}break; 
case 7:{ 
strDate = "星期日" 
}break; 
} 
year = today.getYear(); 
month = today.getMonth()+1; 
date = today.getDate(); 
hour = today.getHours(); 
minute =today.getMinutes(); 
second = today.getSeconds(); 
if(month<10) month="0"+month; 
if(date<10) date="0"+date; 
if(hour<10) hour="0"+hour; 
if(minute<10) minute="0"+minute; 
if(second<10) second="0"+second; 
document.getElementById('time').innerHTML = year + "年" + month + "月" + date + "日 " + strDate +" " + hour + ":" + minute + ":" + second; //显示时间 
setTimeout("showtime();",1000); //设定函数自动执行时间为 1000 ms(1 s) 
} 
// 来自:编程之家 jb51.cc(jb51.cc) 
</script>
<div id="time"></div>
<script language="javascript"> showtime();</script>
</body> 
</html>

原文链接:https://www.f2er.com/js/527792.html

猜你在找的JavaScript相关文章