2014-06-12 26 views
1

我使用jQuery的日期和時間IAM選擇一個日期和時間,所以這將是我在文本框中2014/06/11 18:50在提交按鈕我打電話一個javascript如何在javascript asp.net c#中檢查日期和時間?

<asp:Button ID="bt_schedule" runat="server" CssClass="button" Text="Submit" OnClientClick="return btnSubmit_Click();" 
          OnClick="bt_schedule_Click" /> 
有一個文本框

我的要求是日期應該更大等於和等於今天的日期和時間應該大於當前時間的5分鐘。所以我用類似下面

<script type="text/javascript"> 
      function btnSubmit_Click() { 

       var strval = document.getElementById("<%= tb_calimage.ClientID %>").value; 
       var dateParts = strval.split(" "); 
       var date = dateParts[0]; 
       var time = dateParts[1]; 
       var datecompare = new Date(date); 
       var currentDate = new Date(); 

var sysdate = currentDate.getHours(); 
       var systime = currentDate.getMinutes(); 
       if (datecompare >= currentDate) { 
        alert('Date greater than or equal t0 Today'); 
        return false; 
       } 

       return true; 
      } 

,也爲的currentdate顯示日期與時間,所以我不能夠得到它正確..以及如何隨着時間的推移檢查。請幫助

回答

1

基本上,你當前的字符串格式很好。

所以,如果在Javascript中這樣寫:

var requestedDate = new Date("2014/06/11 18:50"); 

var oldDate = new Date("2014/06/11 18:45"); 

,並嘗試做減法。

var val = requestedDate-oldDate;

val是:300000

擴展這個進一步,你可以只使用var oldDate = new Date()

,並檢查是否val>300000,那麼你有時間從現在開始大於5分鐘:)

完整代碼:

function btnSubmit_Click() { 

       var strval = document.getElementById("<%= tb_calimage.ClientID %>").value; 
       var datecompare = new Date(strval); 
       var currentDate = new Date(); 
       if (datecompare-currentDate <300000) { 
        alert('Please select date-time more than 5 minutes from now.'); 
        return false; 
       } 

       return true; 
      } 
+1

謝謝你這麼多...... :) – user3734234