我实现了一个函数来检查 vb.net 中的时间范围。但它给了我错误的输出。我的开始时间是每天晚上 11 点,结束时间是早上 5 点。如果我通过 1.10 AM,我的函数不会返回真正的输出,因为这属于该时间范围。不知道我做错了什么。
Private Function CheckTimeRange() As Boolean
Dim retValue As Boolean = True
Try
Dim Dt As DateTime = DateTime.Now
Dim StartDt As DateTime = Convert.ToDateTime("11.00 PM")
Dim EndDt As DateTime = Convert.ToDateTime("5.00 AM")
Dim startTime As New TimeSpan(StartDt.Hour, StartDt.Minute, 0)
Dim endTime As New TimeSpan(EndDt.Hour, EndDt.Minute, 0)
Dim now As TimeSpan = DateTime.Now.TimeOfDay
If (now > startTime) AndAlso (now < endTime) Then
retValue = True
Else
retValue = False
End If
Return retValue
Catch ex As Exception
End Try
End Function