JavaScript动态显示当前时间
代码实现:
<body></body>
内容:
<div></div>
<script>
var div = document.querySelector('div');
getDate();
setInterval(getDate, 1000);
function getDate() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = month < 10 ? '0' + month : month;
var dates = date.getDate();
dates = dates < 10 ? '0' + dates : dates;
var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
var day = date.getDay();
var hour = date.getHours();
var half = hour >= 12 ? '下午' : '上午';
hour = hour <= 12 ? hour : (hour - 12);
hour = hour < 10 ? '0' + hour : hour;
var min = date.getMinutes();
min = min < 10 ? '0' + min : min;
var sed = date.getSeconds();
sed = sed < 10 ? '0' + sed : sed;
div.innerHTML = (year + '年' + month + '月' + dates + '日 ' + arr[day] + ' ' + half + hour + '点' + min + '分' + sed + '秒');
}
</script>