比较预订和开放时间

时间:2015-03-10 17:56:02

标签: vb.net

用户通过datetimepickers输入预订的开始时间和结束时间。我希望能够确保预订在大厅的开放时间内。 我试过这个:

MsgBox("start time: " & start_time.value & " End time:" & end_time.value & "")
        If DateTime.Parse(Datetimepicker1.text).DayOfWeek = "1 " Then
            If start_time.value < "08:00:00" Or end_time.value > "22:30:00" Then
                errors.Enqueue("The booking slot must be within the open hours")
            End If
        ElseIf DateTime.Parse(Datetimepicker1.text).DayOfWeek = "2" Then
            If start_time.value < "08:00:00" Or end_time.value > "22:30:00" Then
                errors.Enqueue("The booking slot must be within the open hours")
            End If
        ElseIf DateTime.Parse(Datetimepicker1.text).DayOfWeek = "3" Then
            If start_time.value < "08:00:00" Or end_time.value > "22:30:00" Then
                errors.Enqueue("The booking slot must be within the open hours")
            End If
        ElseIf DateTime.Parse(Datetimepicker1.text).DayOfWeek = "4" Then
            If start_time.value < "08:00:00" Or end_time.value > "22:30:00" Then
                errors.Enqueue("The booking slot must be within the open hours")
            End If
        ElseIf DateTime.Parse(Datetimepicker1.text).DayOfWeek = "5" Then
            If start_time.value < "08:00:00" Then
                errors.Enqueue("The booking slot must be within the open hours")
            End If
        ElseIf DateTime.Parse(Datetimepicker1.text).DayOfWeek = "6" Then
            If start_time.value < "10:00:00" Then
                errors.Enqueue("The booking slot must be within the open hours")
            End If
        ElseIf DateTime.Parse(Datetimepicker1.text).DayOfWeek = "7" Then
            If start_time.value < "10:00:00" Then
                errors.Enqueue("The booking slot must be within the open hours")
            End If
        End If

它适用于星期五,星期六和星期日但不适用于本周的其他日子,这使我认为它与当时的OR运算符有关,但我可以理解为什么。

1 个答案:

答案 0 :(得分:1)

Turn Option Strict On,你不应该将数字与字符串进行比较 不要重复你的代码 DateTimePicker具有.Value属性,您无需解析该字符串。

Dim startTime, endTime As TimeSpan

startTime = Nothing
endTime = Nothing

Select Case Datetimepicker1.Value.DayOfWeek
    Case DayOfWeek.Monday
        startTime = new TimeSpan(8, 0, 0)
        endTime = new TimeSpan(22, 30, 0)
    Case DayOfWeek.Tuesday
        startTime = new TimeSpan(8, 0, 0)
        endTime = new TimeSpan(22, 30, 0)
    Case DayOfWeek.Wednesday
        startTime = new TimeSpan(8, 0, 0)
        endTime = new TimeSpan(22, 30, 0)
    Case DayOfWeek.Thursday
        startTime = new TimeSpan(8, 0, 0)
        endTime = new TimeSpan(22, 30, 0)
    Case DayOfWeek.Friday
        startTime = new TimeSpan(8, 0, 0)
    Case DayOfWeek.Saturday
        startTime = new TimeSpan(10, 0, 0)
    Case DayOfWeek.Sunday
        startTime = new TimeSpan(10, 0, 0)
End Select

If (startTime IsNot Nothing AndAlso Datetimepicker1.Value.TimeOfDay < startTime) Or _
   (endTime IsNot Nothing AndAlso Datetimepicker1.Value.TimeOfDay > endTime) Then
    errors.Enqueue("The booking slot must be within the open hours")
End If

Select Case可以很容易地存放在字典中并保存为设置。