-
Check time range.
Hi,
I'm having a tuff time trying to check if the current time is within a certain range. This is probably very simple, but I can't figure it out.
The AM and PM are not being taken into account...
Code:
If (Date.Now.ToShortTimeString > "3:00AM" And Date.Now.ToShortTimeString < "4:30AM") Then
Response.Redirect("test.aspx")
End If
-
Re: Check time range.
Something like this:
Code:
Dim dtn As DateTime = Now
Dim ts3AM As TimeSpan = New TimeSpan(3, 0, 0)
Dim ts430AM As TimeSpan = New TimeSpan(4, 30, 0)
If (dtn.TimeOfDay > ts3AM And dtn.TimeOfDay < ts430AM) Then
MessageBox.Show("Between 3 and 430")
End If
Note that I store Now in a variable, that way it will be the same for both evaluations.
-
Re: Check time range.
Code:
'ON THE SAME DAY!
Dim stDT As DateTime = DateTime.Parse("3:00:00 AM") 'start
Dim eDT As DateTime = DateTime.Parse("4:30:00 AM") 'end
If DateTime.Now.CompareTo(stDT) >= 0 AndAlso DateTime.Now.CompareTo(eDT) <= 0 Then
Stop
End If