前端开发自学之JavaScript——显示当前时间

时间:2023-03-30 09:45:08
 <html>
<head>
<title>JavaScript</title>
<script language="javascript">
function showtime(){
var now_time = new Date() ; // 创建时间对象
var hours = now_time.getHours() ; //获得当前小时数
var minutes = now_time.getMinutes() ; //获得当前分钟数
var seconds = now_time.getSeconds() ; //获得当前秒数
var timer = "" + ((hours > 12) ? hours - 12 : hours ) ; //将小时数值赋予变量timer
timer += ((minutes < 10) ? ":0" : ":") + minutes ; //将分钟数值赋予变量timer
timer += ((seconds < 10) ? ":0" : ":") + seconds ; //将秒数值赋予变量timer
timer += " " + ((hours > 12) ? "pm" : "am") ; //将字符am或pm赋予变量timer
document.clock.show.value = timer ; //在名为clock的表单中输出变量timer的值
setTimeout("showtime()",1000) ; //设置每隔一秒钟自动调用一次showtime()函数
}
</script>
</head>
<body onload=showtime()>
<form name="clock" onSubmit="0">
<input type="text" name="show" size="10" style="background-color: lightyello;border-width:3;">
</form>
</body>
</html>

javascript也是蛮好玩的啊!

以上为第一个JS小程序实现显示当前时间!