我想通過比較DATES
和TIME
來驗證JavaScript。javascript代碼採用不同格式的日期
我想在dd/MM/yyyy格式,但我不知道它正在採取哪種格式。我調試了js代碼並獲得了格式。下面是我插入TODAYS
日期時給我的任何其他日期的截圖。
以下是代碼: -
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>
正因爲如此,我不能夠做正確的驗證。任何建議如何實現這一點
是否需要安裝'moment.js'插件? – BNN
你必須在你的頁面中包含moment.js腳本 –
你能否給我提供相同的鏈接,因爲大多數網站都阻止了我的結果。 – BNN