JS判斷是否到達指定時間,執行任務,可以精確到秒。

最近有個任務要在JS中判斷一下。網上找瞭好久都沒有解決我的問題。最後直接把JSP代碼COPY出來放入HTML裡面執行。省得每次改後都要編譯。最後測試通過瞭。

貼代碼:

 

[html]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"https://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> 
                            </tr> 
                        </td> 
                    </tr> 
                  </table> 
</body> 
</html> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"https://www.w3.org/TR/html4/loos
e.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> www.aiwalls.com
              <td>當前服務器時間:</td>
              <td>
              <span id="svtime">2012-06-14 17:35</span></td>
             </tr>
           <tr>
             <td>
              <input type="button" value="開始模擬考試" onclick="startexam();"/>
             </td>
             </tr>
         </td>
     </tr>
      </table>
</body>
</html>

作者:yaerfeng

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *