2016-02-09 45 views
0

我想通過比較DATESTIME來驗證JavaScript。javascript代碼採用不同格式的日期

我想在dd/MM/yyyy格式,但我不知道它正在採取哪種格式。我調試了js代碼並獲得了格式。下面是我插入TODAYS日期時給我的任何其他日期的截圖。

Image1

以下是代碼: -

function ValidateRecord() { 
     var StrPriError = ""; 



     if (document.getElementById('TextBox1').value == "" || document.getElementById('TextBox2').value == "" || document.getElementById('TextBox3').value == "" || document.getElementById('TextBox4').value == "") { 
      StrPriError += "Dates cannot be blank \n"; 
     } 
     else { 
      var dt1 = new Date(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value); 
      var dt2 = new Date(document.getElementById('TextBox2').value + " " + document.getElementById('DrpTime3').value); 
      var dt3 = new Date(document.getElementById('TextBox3').value + " " + document.getElementById('DrpTime4').value); 
      var dt4 = new Date(document.getElementById('TextBox4').value + " " + document.getElementById('DrpTime5').value); 

      if (dt1.getTime() == dt2.getTime() || dt1.getTime() == dt3.getTime() || dt1.getTime() == dt4.getTime() || dt2.getTime() == dt3.getTime() || dt2.getTime() == dt4.getTime() || dt3.getTime() == dt4.getTime()) { 
       StrPriError += "Dates and Times cannot be same \n"; 
      } 
      else { 
       // StrPriError += "Good to go \n"; 
      } 

      if (dt2.getTime() < dt1.getTime()) { 
       StrPriError += "Reminder 2 cannot be less than reminder 1 \n"; 
      } 

      if (dt3.getTime() < dt1.getTime()) { 
       StrPriError += "Reminder 3 cannot be less than reminder 1 \n"; 
      } 

      if (dt4.getTime() < dt1.getTime()) { 
       StrPriError += "Reminder 4 cannot be less than reminder 1 \n"; 
      } 
      if (dt3.getTime() < dt2.getTime()) { 
       StrPriError += "Reminder 3 cannot be less than reminder 2 \n"; 
      } 

      if(dt4.getTime() < dt2.getTime()) { 
       StrPriError += "Reminder 4 cannot be less than reminder 2 \n"; 
      } 

      if (dt4.getTime() < dt3.getTime()) { 
       StrPriError += "Reminder 4 cannot be less than reminder 3 \n"; 
      } 
     } 
     if (StrPriError != "") { 
      alert(StrPriError); 
      return false; 
     } 
     else { 
      return true; 
     } 
    } 

也請參閱HTML: -

<td colspan="1px" style="width: 13%" class="field"> 
       <asp:TextBox ID="TextBox1" runat="server" Width="80" Enabled="false"></asp:TextBox> 
       <cc3:Calendar ID="Calendar1" runat="server" DatePickerMode="true" TextBoxId="TextBox1" 
        DatePickerImagePath="../../Images/icon2.gif" CultureName="en-GB"> 
       </cc3:Calendar> 
       <asp:DropDownList ID="DrpTime" runat="server" Width="65px"> 
        <asp:ListItem Value="09:00">09:00</asp:ListItem> 
        <asp:ListItem Value="13:00">13:00</asp:ListItem> 
        <asp:ListItem Value="17:00">17:00</asp:ListItem> 
        <asp:ListItem Value="21:00">21:00</asp:ListItem> 
       </asp:DropDownList> 
      </td> 
      <td colspan="1px" style="width: 13%" class="field"> 
       <asp:TextBox ID="TextBox2" runat="server" Width="80" Enabled="false"></asp:TextBox> 
       <cc3:Calendar ID="Calendar2" runat="server" DatePickerMode="true" TextBoxId="TextBox2" 
        DatePickerImagePath="../../Images/icon2.gif" CultureName="en-GB"> 
       </cc3:Calendar> 
       <asp:DropDownList ID="DrpTime3" runat="server" Width="65px"> 
        <asp:ListItem Value="09:00">09:00</asp:ListItem> 
        <asp:ListItem Value="13:00">13:00</asp:ListItem> 
        <asp:ListItem Value="17:00">17:00</asp:ListItem> 
        <asp:ListItem Value="21:00">21:00</asp:ListItem> 
       </asp:DropDownList> 
      </td> 

正因爲如此,我不能夠做正確的驗證。任何建議如何實現這一點

回答

2

您可以輕鬆地通過使用moment.js實現這一目標,

var dt1 = moment(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value, "DD/MM/yyyy HH:mm"); 
+0

是否需要安裝'moment.js'插件? – BNN

+0

你必須在你的頁面中包含moment.js腳本 –

+0

你能否給我提供相同的鏈接,因爲大多數網站都阻止了我的結果。 – BNN

2
var monthNames = [ 
         "January", "February", "March", 
         "April", "May", "June", "July", 
         "August", "September", "October", 
         "November", "December" 
         ]; 

       var dd = new Date(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value); 

       output.value = dd.getDate() + ' ' + monthNames[dd.getMonth()] + ' ' + dd.getFullYear() + ' ' + dd.getHours() + ':' + dd.getMinutes() + ':' + dd.getSeconds(); 

我認爲上述代碼將幫助你。

+0

讓我嘗試和檢查 – BNN

+0

得到錯誤爲'微軟JScript運行時錯誤:「輸出」是undefined' – BNN

+0

是什麼出來的實際字符串值日期文本框 – Bindrid