我在前端使用了以下JavaScript代码进行验证:
function ConvertTime() {
var inputval = "0" + document.getElementById('FromTime').value;
var inputval2 = "0" + document.getElementById('ToTime').value;
var tokens = /([10]?\d) : ([0-5]\d) ([ap]m)/i.exec(inputval);
if (tokens == null) {
return null;
}
if (tokens[3].toLowerCase() === 'pm' && tokens[1] !== '12') {
tokens[1] = '' + (12 + (+tokens[1]));
} else if (tokens[3].toLowerCase() === 'am' && tokens[1] === '12') {
tokens[1] = '00';
}
var tokens2 = /([10]?\d) : ([0-5]\d) ([ap]m)/i.exec(inputval2);
if (tokens2 == null) {
return null;
}
if (tokens2[3].toLowerCase() === 'pm' && tokens2[1] !== '12') {
tokens2[1] = '' + (12 + (+tokens2[1]));
} else if (tokens2[3].toLowerCase() === 'am' && tokens2[1] === '12') {
tokens2[1] = '00';
}
var convertedval = tokens[1] + ' : ' + tokens[2];
var convertedval2 = tokens2[1] + ' : ' + tokens2[2];
if (convertedval2 <= convertedval) {
alert("End time must be greater than start time!");
alert(convertedval + " " + convertedval2)
}
}
<div>
<label for="FromTime">From</label>
<input type="text" class="timepicker form-control" id="FromTime" name="FromTime">
</div>
<div>
<label for="ToTime">To</label>
<input type="text" class="timepicker form-control" id="ToTime" name="ToTime">
</div>
<br />
<input type="submit" value="Allocate" class="btn btn-primary" id="button" onclick="ConvertTime()" />
因此,上述代码实际上以24小时时间格式检查结束时间(ToTime)是否大于开始时间(FromTime)。然后再次在服务器端,我使用以下内容来检查C#的24小时时间格式:
int FromTime = DateTime.Parse(aAllocateRoom.FromTime).Hour;
int ToTime = DateTime.Parse(aAllocateRoom.ToTime).Hour;
if (ToTime > FromTime && FromTime < ToTime)
{
//Code
}
所以输入如下:
FromTime - 10:00 AM
ToTime - 11:00 AM
因此,当时间输入如下时验证:
// Alerts FromTime is greater than ToTime as in 24 hour format 1:00 PM is 13:00
FromTime - 01:00 PM
ToTime - 11:00 AM
现在我想要的是我希望提供以下输入:
FromTime - 01:00 PM
ToTime - 11:00 AM
或者
FromTime - 11:00 AM
ToTime - 01:00 PM
但仅在这些情况下进行验证:
i) FromTime - 01:00 PM //Not possible - Same for AM
ToTime - 01:00 PM
ii) FromTime - 01:00 PM //Not possible
ToTime - 12:00 PM
iii) FromTime - 01:00 AM //Not possible
ToTime - 12:00 AM
我想知道上述验证是否可以在JavaScript和C#中使用。
答案 0 :(得分:1)
尝试
DateTime FromTime = DateTime.ParseExact(aAllocateRoom.FromTime,
"hh:mm tt", CultureInfo.InvariantCulture);
DateTime TomTime = DateTime.ParseExact(aAllocateRoom.ToTime,
"hh:mm tt", CultureInfo.InvariantCulture);
if (FromTime < ToTimie)
{
//Code
}
答案 1 :(得分:1)
System.Globalization.CultureInfo provider = new System.Globalization.CultureInfo("tr-TR");
DateTime Fromtimei=DateTime.ParseExact("130000","HHmmss",provider);
DateTime Totimei=DateTime.ParseExact("130000","HHmmss",provider);
DateTime Fromtimeii=DateTime.ParseExact("130000","HHmmss",provider);
DateTime Totimeii=DateTime.ParseExact("120000","HHmmss",provider);
DateTime Fromtimeiii=DateTime.ParseExact("010000","HHmmss",provider);
DateTime Totimeiii=DateTime.ParseExact("000000","HHmmss",provider);
Console.WriteLine(DateTime.Compare(Fromtimeiii,Totimeiii));
if(DateTime.Compare(Fromtimei,Totimei)==0)
{
Console.WriteLine(Fromtimei.ToString()+"\n"+Totimei.ToString() +"\nNot possible - Same for AM\n\n");
}
if(DateTime.Compare(Fromtimeii,Totimeii)==1)
{
Console.WriteLine(Fromtimeii.ToString()+"\n"+Totimeii.ToString() +"\nNot possible\n\n");
}
if(DateTime.Compare(Fromtimeiii,Totimeiii)==1)
{
Console.WriteLine(Fromtimeiii.ToString()+"\n"+Totimeiii.ToString() +"\nNot possible\n\n");
}
输出:
2016年8月17日下午1:00:00 8/17/2016 1:00:00 PM 不可能 - AM相同
2016年8月17日下午1:00:00 8/17/2016 12:00:00 PM 不可能
2016/8/17 1:00:00 8/17/2016 12:00:00 AM 不可能