vb.net中的日期时间验证

时间:2010-04-17 09:12:56

标签: vb.net validation datetime

任何人都可以帮助我如何使用vb.net验证两个DateTime之间的给定DateTime?

给定的DateTime不能介于两个DateTime ..

之间

3 个答案:

答案 0 :(得分:4)

您可以将date instances与数字进行比较,因为它们会覆盖LessThanLessThanOrEqualGreaterThanGreaterThanOrEqual运算符:

Dim minDate = New DateTime(2009, 4, 17)
Dim maxDate = New DateTime(2011, 4, 17)
Dim selectedDate = New DateTime(2010, 4, 17)

If selectedDate > minDate And selectedDate < maxDate Then
    Console.WriteLine("selected date is valid")
Else
    Console.WriteLine("selected date is invalid")
End If

答案 1 :(得分:1)

使用DateTime.Compare

Dim date1 As New DateTime(100)
Dim date2 As New DateTime(200)
Dim workingDate As New DateTime(150)    

If DateTime.Compare(workingDate , date1) >= 0 AND DateTime.Compare(workingDate , date2) <= 0 Then
    Console.WriteLine("Between")
Else
    Console.WriteLine("Not Between")
End If

答案 2 :(得分:0)

Dim dt As Date = DateTimePicker1.Value.Date
If dt < CDate("2010-04-01") Or dt > CDate("2010-04-10") Then
    MessageBox.Show("Date is not in the first 10 days of April so is ok")
End If