随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
JS中包含Date对象,其提供了一些方法获取系统日期,直接上代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>获取系统时间</title>
<script language="JavaScript">
function realSysTime(clock) {
var now = new Date();
var year = now.getFullYear(); //获取年份
var month = now.getMonth(); //获取月份
var date = now.getDate(); //获取日期
var day = now.getDay(); //获取星期
var hour = now.getHours(); //获取小时
var minute = now.getMinutes(); //获取分钟
var seconds = now.getSeconds(); //获取秒
month = month + 1;
var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
var week = arr_week[day];
var time = year + "年" + month + "月" + date + "日 " + week + " " + hour + ":" + minute + ":" + seconds;
clock.innerHTML = "当前时间:" + time;
}
function show() {
window.setInterval("realSysTime(clock)", 1000);
}
</script>
</head>
<body onload="show()" wind>
<div id='clock'></div>
</body>
</html>