当前位置:编程学习 > JS >>

JS判断是否到达指定时间,执行任务,可以精确到秒。

最近有个任务要在JS中判断一下。网上找了好久都没有解决我的问题。最后直接把JSP代码COPY出来放入HTML里面执行。省得每次改后都要编译。最后测试通过了。

贴代码:

 


[html]
<!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="开始模拟考试" onclick="startexam();"/> 
                            </td> 
          &nbs

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,