+ Reply to Thread
Results 1 to 27 of 27

Formula to work out day/night based on start and end hours

  1. #1
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Formula to work out day/night based on start and end hours

    Good Morning All,
    I need a little help with some data. I have a spread-sheet with over 10,000 rows of data. The attached file shows the headings and type of data in the cells. I'm struggling with a formula for column G (day/night). What I need to do is populate this column with 'night' but only where:

    The start date and end date is the same and the issue occurred after 9pm (using start time column)The end time is equal to 5am or less but again where the start and end date is the same (based on end time)The difference between the start and end date is one day and the issue occurred at or after 9pm on the start date but at or before 5am on the end date.

    I have no idea if this makes any sense but please let me know if further clarification is needed!

    Once that is done, I then need to work out what took place on a Friday, or Friday into Saturday, Saturday or Saturday into Sunday etc.
    Any, and I mean any help would be appreciated.
    Thank you all in advance!
    A Frustrated Excel User


    Attachment 489841
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    08-23-2016
    Location
    India
    MS-Off Ver
    MS Office Professional Plus 2013
    Posts
    194

    Re: Formula to work out day/night based on start and end hours

    Use this in column G2.......

    =IF(AND(A2=B2,C2>=TIME(21,0,0)),"Night","")
    Ash

  3. #3
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Formula to work out day/night based on start and end hours

    Try
    Please Login or Register  to view this content.
    If someone has helped you then please add to their Reputation

  4. #4
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    Thank you so much for this. It works and I'm one happy bunny Really appreciate your help/

  5. #5
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Formula to work out day/night based on start and end hours

    Quote Originally Posted by pritstick View Post
    Thank you so much for this. It works and I'm one happy bunny Really appreciate your help/
    I was just posting, I dont think it does work....it did for your test data but none of thise in there have a start and end time between midnight and 5am.

  6. #6
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    Thank you so much!

  7. #7
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    Ahhhh, yes, I didn't want to include too much data on the attached file. It works for data included but I will hold fire for now. Thanks for spotting this!

  8. #8
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    You're right...it doesn't work. Just tried it where the times are between 1am and 2am.

  9. #9
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,178

    Re: Formula to work out day/night based on start and end hours

    You cannot have an end time after midnight on the same day!!

    If any shift starts after 9 pm, irrespective of end time, is it a "night" shift? so only test "Start Time"

    Could a "night" shift finish as 06:00 ? If so, how is the 05:00 - 06:00 classified?

  10. #10
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    Hi John and thanks for your reply. Yes, it should be 9pm to 23.59 really and then midnight to 5am, all of which would be classified as night. 5am to 6am would not be classified as night in this instance. We are really looking at anything between 9pm and 5am really to be classified as night.

  11. #11
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,178

    Re: Formula to work out day/night based on start and end hours

    Try

    =IF(ISNUMBER(C2),IF(OR(C2>TIME(21,0,0),AND(C2>=TIME(0,0,0),C2<=TIME(5,0,0))),"Night",""),"")

    NOTE: row 18 appears "odd" as the dates are 2 days apart .

  12. #12
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    Hi John,
    Yes, some of my data includes dates that are over 2 days old. Thank you for the formula which I will try asap!

  13. #13
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    Ok, I don't think I have explained my issue well enough as none of these formulas are not working
    Let me give this another go:
    If F2 = 1 and the start time is greater than or equal 21:00 and the end time is less than or equal to 05:00 hours, populate as night OR if F2 is equal to 0 and the start time is greater than or equal to 21:00 and the end time is less than or equal to 23.59 , then populate as night OR if F2 =0 and the start time is greater than or equal to 00:00 and the end time is less than or equal to 05:00, then populate as night.

  14. #14
    Forum Contributor
    Join Date
    08-23-2016
    Location
    India
    MS-Off Ver
    MS Office Professional Plus 2013
    Posts
    194

    Re: Formula to work out day/night based on start and end hours

    try this...........

    =IF(AND(A2-B2<2,C2>=TIME(21,0,0),OR(D2<=TIME(5,0,0),D2>=TIME(21,0,0))),"Night","")

  15. #15
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    Hi Ash and thank you for replying. I've tried this and it doesn't work It should say 'night' where the start date is 02/10/16 and end date is 03/10/16 and start time is 01:00 and end time is 02:00 but it does not populate accordingly. Thank you for trying.

  16. #16
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Formula to work out day/night based on start and end hours

    Another go
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    Hi pjwhitfield...I think this works!! I just need to try it on a number of different combinations but it's looking positive so far. Thank you soooooooooooo much for your all your assistance with this matter. It really is appreciated.
    Last edited by pritstick; 11-18-2016 at 08:03 AM.

  18. #18
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Formula to work out day/night based on start and end hours

    Im still a wee bit tentative about it so would appreciate a good testing.

  19. #19
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    I completely understand and will give it some rigorous testing. Again, thank you all! I may be back yet...

  20. #20
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    Me again....told you I might be back. The formula works with the majority of dates/times but when I had the start date as 01/01/16, end date as 02/01/16, start time as 21:00 and end time as 09:00, it populated it as night when really, it should have been blank. You did warn me...

  21. #21
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Formula to work out day/night based on start and end hours

    Quote Originally Posted by pritstick View Post
    Me again....told you I might be back. The formula works with the majority of dates/times but when I had the start date as 01/01/16, end date as 02/01/16, start time as 21:00 and end time as 09:00, it populated it as night when really, it should have been blank. You did warn me...

    yeah, I think that currently it works if One of the times are correct but still shows night if one of them is outside of allowed.


    Change of method now.....
    Please Login or Register  to view this content.

  22. #22
    Forum Contributor
    Join Date
    08-23-2016
    Location
    India
    MS-Off Ver
    MS Office Professional Plus 2013
    Posts
    194

    Re: Formula to work out day/night based on start and end hours

    Does this help?

    =IF(AND(F2<=1,OR(C2>=TIME(21,0,0),C2<=TIME(5,0,0)),OR(D2<=TIME(5,0,0),D2>=TIME(21,0,0)),C2<>"",D2<>""),"Night","")

  23. #23
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Formula to work out day/night based on start and end hours

    On the plus side, this new one posted seems to be working fine, although I will test it further next week and may be back in touch My weekend starts here so again, I can't thank you enough for your time and patience. Maybe speak next week!

  24. #24
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Formula to work out day/night based on start and end hours

    Its been an interesting one, thanks for posting it.

    Ash, I think your version suffers from the same thing as my previous one ie if EITHER Start or Finish Time is within the range then it shows night but BOTH must be.

  25. #25
    Forum Contributor
    Join Date
    08-23-2016
    Location
    India
    MS-Off Ver
    MS Office Professional Plus 2013
    Posts
    194

    Re: Formula to work out day/night based on start and end hours

    Can you please give few more examples in this new attachment. Tell what should be NIGHT and what shouldn't be.
    Attached Files Attached Files

  26. #26
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25
    Quote Originally Posted by pjwhitfield View Post
    Its been an interesting one, thanks for posting it.

    Ash, I think your version suffers from the same thing as my previous one ie if EITHER Start or Finish Time is within the range then it shows night but BOTH must be.
    No, thank you for taking the time to look into it for me.

  27. #27
    Registered User
    Join Date
    10-28-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    25
    Quote Originally Posted by Ash_Maverick View Post
    Can you please give few more examples in this new attachment. Tell what should be NIGHT and what shouldn't be.
    Hi Ash,
    Thank you for your help too. I can't see my file anymore as I'm no longer in the office but night should be anything where:
    The start and end dates are the same and the time falls between 21:00 and 23.59
    Where the the start and end dates are the same and where the time is between midnight and 05:00
    And finally, where the start and end dates are only one day apart and where the time falls between 21:00 and 05:00.
    The attachment I posted probably wasn't the best example really as those dates and time are all over the place. I think the formula posted by pjwhitfield will do the trick. Thank you again.

+ 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. Trying to Calculate day and night hours based on sunset and sunrise
    By davidhale87 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-27-2023, 05:33 PM
  2. Replies: 1
    Last Post: 01-11-2016, 05:34 PM
  3. [SOLVED] Adding work hours based on particular start or finish times
    By RobertM01 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-16-2015, 10:28 AM
  4. Replies: 3
    Last Post: 06-18-2014, 07:26 AM
  5. Replies: 4
    Last Post: 09-21-2012, 12:58 AM
  6. finding night hours (0-day,1-night) between ranges of data
    By sensation in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-09-2012, 09:08 AM
  7. Calculate START date based on duration (work hours) and END date
    By kaaver in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-06-2010, 12:21 PM

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