Results 1 to 7 of 7

Thread: [RESOLVED] Is Current Time Between 9pm and 3am?

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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

  3. #3

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    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.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  5. #5
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    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

  6. #6

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    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.

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.
    Last edited by leinad31; Jun 19th, 2007 at 10:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width