<html>
<head>
<title>当前时间</title>
<script type="text/javascript">
//显示时间
function showClock(){
var time = new Date(); //获取当前时间
var year = time.getFullYear(); //获取当前年份
var month = time.getMonth() + 1; //获取当前月份(getMonth()的取值范围为0~11)
var date = time.getDate(); //获取当前日期
var hour = time.getHours(); //获取当前小时数
var minute = time.getMinutes(); //获取当前分钟数
var second = time.getSeconds(); //获取当前秒数
var millisecond = time.getMilliseconds(); //获取毫秒数
month = checkTime(month); //检查月份,若为个位数前面补零
date = checkTime(date); //检查日期,若为个位数前面补零
minute = checkTime(minute); //检查分钟数,若为个位数前面补零
second = checkTime(second); //检查秒数,若为个位数,前面补零
ap = checkNoon(hour); // 检查时am还是pm
document.getElementById("clock").innerHTML = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second + "." + millisecond + " " + ap;
setTimeout("showClock()",100);
}
//对时间是一位数的进行前面补零
function checkTime(i)
{
if (i < 10) {
i = "0" + i;
}
return i;
}
//判断是上午还是下午
function checkNoon(h){
if(h < 12){
ap = "am";//上午
}
else{
ap = "pm";//下午
}
return ap;
}
</script>
</head>
<body onload = "showClock()"> //调用showClock()函数
<div id = "clock"></div>
</body>
</html>
用js来显示当前时间
最新推荐文章于 2024-07-18 11:50:26 发布