需求
在页面上动态展示当前时间
思路
获取当前事件,定时刷新
DEMO
<!DOCTYPE html>
<html>
<head>
<title>showTime</title>
</head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function () {
// 每隔1000毫秒(1秒)刷新一次
setInterval("showTime()", 1000);
})
function showTime() {
/* 获取当前时间 */
var date = new Date();
/* 由上至下为年、月、日、时、分、秒 */
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
/* 格式化分、秒,如果小于10则在前面拼接一个0 */
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
/* 拼接日期、时间 */
var showDate = year + "年" + month + "月" + day + "日";
var showTime = hour + ":" + minute + ":" + second;
/* 展示 */
$('#showDate').html(showDate);
$('#showTime').html(showTime);
}
</script>
<body>
<div id="showDate"></div>
<div id="showTime"></div>
</body>
</html>
新手一枚,不足之处,望不吝赐教。