<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function showTheTime(){
var dayName = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var now = new Date(); //通过 new 关键词来定义 Date 对象 Date 对象自动使用当前的日期和时间作为其初始值
document.getElementById("showTime").innerHTML = showTheHours(now.getHours())+showZeroFilled(now.getMinutes())+showAmPm(now.getHours())+dayName[now.getDay()];
function showTheHours(theHour){ //参数为当前时段
if(show24Hour() || (theHour >0 && theHour < 13)){ //如果show24Hour()为ture 或者 (时段大于0并且小于13>)
return theHour; //返回当前时段
}
if(theHour == 0){ //如果时段为0
return 12; //返回12 表示午夜
}
return theHour-12; //如果时段大于等于13 减去12 中午以后的时间转为12小时制
};
function showZeroFilled(inValue){ //参数为当前分钟数
if(inValue > 9){ //如果分钟时大于9
return ":" + inValue; //冒号加上当前分钟数
}
return ":0" + inValue; //冒号0加上当前分钟数
};
function show24Hour(){
return document.getElementById("show24").checked; //返回选中id为show24的单选按钮
};
function showAmPm(amPm){
if(show24Hour()){ //如果选中了24小时制
return ""; //返回空
}
if(amPm < 12){ //如果时段小于12
return "Am"; //返回Am
}
return "Pm"; //如果大于等于12返回Pm
};
};
window.onload = showTheTime
</script>
</head>
<body>
<div class="centered">
<h2 id="showTime"></h2>
Display 24-hour Clock?
<input type="radio" name="timeClock" id="show24" /><label for="show24">Yes</label>
<input type="radio" name="timeClock" id="show12" /><label for="show12">No</label>
</div>
</body>
</html>