<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>判断是否到达指定时间,可以精确到秒</title>
<SCRIPT type="text/javascript">
function $$(id){
return document.getElementById(id);
}
function dateDiff(date1, date2)
{
date1 = date1.replace("年","-").replace("月","-").replace("日","");
date2 = date2.replace("年","-").replace("月","-").replace("日","");
date1 = new Date(date1.replace(/-/g, "/"));
date2 = new Date(date2.replace(/-/g, "/"));
if(Date.parse(date2) - Date.parse(date1) >= 0){
return true;
}
return false;
}
function startexam(){
var sstime=$$('sstime').innerHTML;
var eetime=$$('eetime').innerHTML;
var svtime=$$('svtime').innerHTML;
var res=dateDiff(sstime, svtime);
alert(res);
if(!res){
alert('还没有到考试时间。');
return;
}else{
alert('已过考试开始时间。流程继续。');
}
var res2=dateDiff(eetime, svtime);
if(res2){
alert('此场次考试已过期。');
return;
}
alert('开始考试。');
}
</SCRIPT>
</head>
<body>
<table>
<tr >
<td>考试开始时间</td>
<td>
<span id="sstime">2012-06-14 17:00</span>
</td>
</tr>
<tr>
<td>考试结束时间:</td>
<td>
<span id="eetime">2012-06-14 17:34</span>
</td>
</tr>
<tr>
<td>当前服务器时间:</td>
<td>
<span id="svtime">2012-06-14 17:35</span></td>
</tr>
<tr>
<td>
<input type="button" value="开始模拟考试" οnclick="startexam();"/>
</td>
</tr>
</td>
</tr>
</table>
</body>
</html>