+ Reply to Thread
Results 1 to 5 of 5

Thread: How to detect time between 18:00 and 06:00?

  1. #1
    Registered User
    Join Date
    10-17-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    3

    How to detect time between 18:00 and 06:00?

    Hi,
    I'm trying to write an if statement to show "day" if it is between 06:00 and 18:00 and "night" if it is between 18:00 and 06:00.

    This is harder for me than perhaps it should be! Any ideas?

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,223

    Re: How to detect time between 18:00 and 06:00?

    Try this:

    =IF(OR(A1>0.75,A1<0.25), "Night", "Day")

    0.75 is the decimal equivalent of the time formatted as "18:00".
    0.25 is the decimal equivalent of the time formatted as "6:00".
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    10-17-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: How to detect time between 18:00 and 06:00?

    This works perfectly, but...I was hoping to combine it with a cell containing the now() function, but for some reason it doesn't like that - even when the cell is formatted as hh:mm.

    Any ideas?

    **EDIT** Fixed this myself, wrapped now() in a mod(now(),1) and it worked. Thanks!

    Quote Originally Posted by JBeaucaire View Post
    Try this:

    =IF(OR(A1>0.75,A1<0.25), "Night", "Day")

    0.75 is the decimal equivalent of the time formatted as "18:00".
    0.25 is the decimal equivalent of the time formatted as "6:00".
    Last edited by drx; 10-17-2011 at 05:38 PM. Reason: Solved

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,223

    Re: How to detect time between 18:00 and 06:00?

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Forum Moderator daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2007
    Posts
    10,056

    Re: How to detect time between 18:00 and 06:00?

    Quote Originally Posted by drx View Post
    This works perfectly, but...I was hoping to combine it with a cell containing the now() function, but for some reason it doesn't like that - even when the cell is formatted as hh:mm.
    For NOW()....

    =LOOKUP(HOUR(NOW()),{0,6,18,"Night","Day","Night"})

    or

    =IF(MATCH(HOUR(NOW()),{0,6,18})=2,"Day","Night")
    Last edited by daddylonglegs; 10-17-2011 at 06:14 PM.
    Audere est facere

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0