我希望通过比较DATES
和TIME
来验证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>
因此我无法正确进行验证。任何建议如何实现这个
答案 0 :(得分: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();
我认为以上代码可以帮助您。
答案 1 :(得分:2)
您可以使用moment.js,
轻松实现此目的var dt1 = moment(document.getElementById('TextBox1').value + " " + document.getElementById('DrpTime').value, "DD/MM/yyyy HH:mm");