I have a feature on my system that displays a message based on the time of day, but it doesn't work
Dim msg As String
Dim horaActual As String
horaActual = System.DateTime.Now.ToString("HH:mm")
If horaActual >= "24:00" and horaActual <= "12:00" Then
msg = "Buenos Días"
ElseIf horaActual >= "12:01" and horaActual <= "19:00" Then
msg = "Buenas Tardes"
ElseIf horaActual >= "19:01" and horaActual <= "23:59" Then
msg = "Buenas Noches" End code
End If
I get the system time (9:16) and it should fit the first condition but it doesn't fit any.
Another way to compare the dates is with the expression of
DateTime
with the sign#
, it can be like this:Here's a screenshot of how it works:
The problem I notice is that you want to compare ranges using string, when you should use numbers
You could convert the time to military format (which you have surely heard in many movies) where 12:00 is 1200. For this you take the time and convert it to a number.
The solution is that apparently 24:00 Hrs is actually 00:00 Hrs
See, I replicated your code by changing the
msn
byLabel1.Text =
(for easy replicating) and it works fine:Label2 is the one on top and contains the current time.