Reputation: 11
How do I check if my current time is between 06:00:00 AM and 11:00:00 AM?
Is it something like this?
If Now.ToString("hh:mm:ss tt") >= "06:00:00 AM" and Now.ToString <= "11:00:00 AM" Then
'Do something
End If
I'm sorry. I'm still learning.
Upvotes: 1
Views: 8325
Reputation: 13
You're very close. I would do it with 24 hour clock to avoid ambiguity:
Dim timeNow As String = Now.ToString("HH:mm:ss")
If timeNow >= "06:00:00" And timeNow <= "11:00:00" Then
'Do something`
End If
Upvotes: 1
Reputation: 11
Good. Here is an updated version. Convert 24hour clock time to an integer then do logic on Integers
Dim timeValue As Integer = Now.ToString("HHmm")
If timeValue >= 600 and timeValue <= 1100 Then
' do it now
End If
Upvotes: 0
Reputation: 43743
I feel like there ought to be an easier way to do this, but if there is, I can't think of it. Here's the easiest/safest way I came up with to do it:
Dim curr As Date = Date.Now
Dim startTime As New Date(curr.Year, curr.Month, curr.Day, 6, 0, 0)
Dim endTime As New Date(curr.Year, curr.Month, curr.Day, 11, 0, 0)
If (curr >= startTime) And (curr <= endTime) Then
' Do something
End If
Upvotes: 2