+ Reply to Thread
Results 1 to 7 of 7

Time period of the day - advanced if statement

  1. #1
    Registered User
    Join Date
    10-21-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    21

    Time period of the day - advanced if statement

    Hey.

    I am trying to make a pretty advance IF statement and I have just ended up with a big headache trying to understand how IF statements work in Excel.

    I am basically trying to say if its during the night (22.00 - 06.59) or during the day (07.00-21:59) so I use the HOUR formula to convert my time to a number between 0 and 23 and this works fine.

    However i have a start time (x) and end time (y) so it becomes a bit complicated.

    I am basically trying to write this in excel:
    //x = HOUR(H2)
    //y = HOUR(I2)

    IF
    (x>=22 and x<=23) or (x>=0 and x<=6)
    and
    (y>=22 and y<=23) or (y>=0 and y<=6)
    //if true write "Night" else "Day"

    So if x is between 22 and 23 or 0 and 6 its during the day
    but i also want to check the end time is also between these times so I do the same for y.

    The problem is when I try to write this in excel.
    I have come up with something like:
    =IF(OR(AND
    (HOUR(H2)>=22;HOUR(H2)<=23);
    AND(HOUR(H2)>=0;HOUR(H2)<=6);
    OR
    (AND(HOUR(I2)>=22;HOUR(I2)<=23);
    AND(HOUR(I2)>=0;HOUR(I2)<=6))
    );"Night";"Day")

    This formula works (amazingly)
    But it only check x and y individually and don't compare them. It might be an easy fix from this but my head is exploding so would really appreciate some help. It probably is a way better way of solving this.

    Thanks in advance.
    Last edited by stibay; 10-23-2013 at 03:33 AM.

  2. #2
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Time period of the day - advanced if statement

    So, it seems like you have a formula that properly calculates the logic you desire, as stated. Then you say 'But it only check x and y individually and don't compare them.' I guess that is the problem, but I don't understand what 'compare them' means and what you want. Please explain.
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

  3. #3
    Registered User
    Join Date
    10-21-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: Time period of the day - advanced if statement

    Quote Originally Posted by Pauleyb View Post
    So, it seems like you have a formula that properly calculates the logic you desire, as stated. Then you say 'But it only check x and y individually and don't compare them.' I guess that is the problem, but I don't understand what 'compare them' means and what you want. Please explain.
    Hmm ok, I dont think compare was the right word for it.

    But I can try to explain it abit better.

    In the database I have 2 columns thats relevant for this if statement, start_time and end_time (x and y)
    The data is date format but I have fixed it so I just have the start time and end time in 2 columns (format 00:00 - 23:59)

    And with the HOUR() formula i mentioned before I get a number from 0-23 from those times (don't know if this is the best way but seems to work atleast)

    So then I want to check if the time is between 22:00 and 06:59 (or 7, doesnt matter that much, but for the HOUR formula 6:59 = 6)
    So what I have to check first if its between 22-24, so thats 22 - 23 and then if its from 24:00 to 06:00, so 0-6.

    And if that wasnt enough I want to check that for BOTH X and Y (so I wont get records like 23:00 to 15:45 or 03:00 to 16:00 as "Night" f.ex)
    So if X is between 22-06 (22-23 and 0-6) AND Y is between 22-06 (22-23 and 0-6) Then i want to write "Night" Else "Day"

    Hope this made it abit clearer

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,936

    Re: Time period of the day - advanced if statement

    Hi and welcome to the forum

    To keep things simple, test with actual times (excel sees time as a decimal of 1 day - 12:00 mid-day is actually 0.5 etc), use 7/24 and 22/24
    Also, test for the "day-time", and anything else will be night time
    =if(and(A1>=7/24,A1<=22/24),"do this","do that")
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  5. #5
    Registered User
    Join Date
    10-21-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: Time period of the day - advanced if statement

    Quote Originally Posted by FDibbins View Post
    Hi and welcome to the forum

    To keep things simple, test with actual times (excel sees time as a decimal of 1 day - 12:00 mid-day is actually 0.5 etc), use 7/24 and 22/24
    Also, test for the "day-time", and anything else will be night time
    =if(and(A1>=7/24,A1<=22/24),"do this","do that")
    Hi.

    My statement is only test for "one thing" if not everything else.

    The problem is that I want to check both X and Y if its between 22 and 6. (See my last post) So how would you include that in your solution?

  6. #6
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Time period of the day - advanced if statement

    So, you need another AND wrapper around your formula? I also think you have a bug in your formula since you have 3 entries for your first OR statement. Something like this?
    =IF(AND(OR(AND(HOUR(H2)>=22,HOUR(H2)<=23),AND(HOUR(H2)>=0,HOUR(H2)<=6)),OR(AND(HOUR(I2)>=22,HOUR(I2)<=23),AND(HOUR(I2)>=0,HOUR(I2)<=6))),"Night","Day")

  7. #7
    Registered User
    Join Date
    10-21-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: Time period of the day - advanced if statement

    Quote Originally Posted by Pauleyb View Post
    So, you need another AND wrapper around your formula? I also think you have a bug in your formula since you have 3 entries for your first OR statement. Something like this?
    =IF(AND(OR(AND(HOUR(H2)>=22,HOUR(H2)<=23),AND(HOUR(H2)>=0,HOUR(H2)<=6)),OR(AND(HOUR(I2)>=22,HOUR(I2)<=23),AND(HOUR(I2)>=0,HOUR(I2)<=6))),"Night","Day")
    Thanks alot That is working perfect

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 07-25-2013, 10:03 AM
  2. [SOLVED] Help taking a time from one field and outputting a time period in a different column
    By Hidden_Gecko in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 02-07-2013, 12:42 AM
  3. [SOLVED] Complicated:Calculate time period based on no. of units sold + fluctuations within period
    By omaha.crab in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2012, 03:04 AM
  4. Specific time period calculation from a time range
    By Khaldon in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-18-2011, 06:44 AM
  5. Replies: 2
    Last Post: 04-27-2011, 08:21 AM

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.6.0 RC 1