[RESOLVED] Is Current Time Between 9pm and 3am?
Hello everybody,
I have two times 9pm and 3am. I want to see if the current time is between these two times.
Currently I am trying to do it in this manner, which doesn't work. Is there another way without having to resort to checking if the current time is before or after Midnight?
Code:
If (TimeValue(Time) >= TimeValue(lbl9PM)) And (TimeValue(Time) < TimeValue(lbl3AM)) Then
Re: Is Current Time Between 9pm and 3am?
Check if the time isn't between 3am and 9pm.
Code:
If Not (Time >= TimeValue(lbl3AM) And Time < TimeValue(lbl9PM)) Then
Debug.Print "Do Something"
End If
Re: Is Current Time Between 9pm and 3am?
Thanks for your assistance, however when I stated the problem I just mentionned the basic problem, in the actual case there are several intervals throughout the day i.e.
3am
7am
9am
11am
1pm
7pm
9pm
etc...
I check to see whether the current time is between which interval. My if condition works with all intervals except the 9pm to 3am interval. What you proposed won't solve this problem.
Re: Is Current Time Between 9pm and 3am?
On a 24 hour clock basis (no date), there is no time that's both greater than 9PM and less than 3AM. You have to check whether the time is between 9PM and 11:59:59PM or between 00:00:00AM and 3AM. If either one is true, the time is between 9PM and 3AM.
Re: Is Current Time Between 9pm and 3am?
You need only to use OR instead of AND in your comparison.
Code:
If (TimeValue(Time) >= TimeValue(lbl9PM)) Or (TimeValue(Time) < TimeValue(lbl3AM)) Then
Re: Is Current Time Between 9pm and 3am?
Thanks for everybody's help. In the end what I did was check all intervals that fall on the same day, if it is not any of them then it is in the interval of between 9pm and 3am.
Re: [RESOLVED] Is Current Time Between 9pm and 3am?
People often overlook day spanning when tackling duration, they often just consider duration within the day (often duration during daytime)... for intervals above consider them all in day zero, or a datetime variable with value from 0 to less than 1 (unit of measure of datetime data type is a day, decimal portion or less than 1 or part of a day is the time info)... so 3AM (day zero) can be created with datetime_variable = TimeSerial(3). 3AM the next day (plus one day) would be datetime_variable = 1 + TimeSerial(3).
So you only needed to add 3AM the next day in your list of intervals using datetime data type, or you only needed to add to the list your same starting time but for the next day, in order to make the list span 24 hours. When time being considered is less than start time then just add 1 to it, it would then fall on last interval.