Excel IF AND formula between two times - excel

I would like to have a formula which will tell me if a time in a cell is between 2 separate vlaues in other cells and to return a value if so.
I have already created the below code but this is not returning any values back at all.
=IF(AND(F4>=$R$1,F4<P1),"Night Shift",IF(AND(F4>=$P$1,F4<$Q$1),"AM Shift",IF(AND(F4>=$Q$1,F4<$R$1),"PM Shift","")))
In this example the cell values are (P1 = 06:00, Q1 = 14:00, R1 = 22:00). The value in the F4 is 00:31:38.
Any help would be appreciated.

Your first AND needs to adjust a little.
Excel sees TIME as a fraction of 1 whole day. So 00:31:38 though you meant it to be the next day from 22:00, Excel does not know that and as such will not see it greater than 22:00
We also do not need to test for the Night Shift. It is the only option left if the time is not in one of the others:
=IF(F4<>"",IF(AND(F4>=$P$1,F4<$Q$1),"AM Shift",IF(AND(F4>=$Q$1,F4<$R$1),"PM Shift","Night Shift")),"")
You could also create a small table like such:
0 6:00 14:00 22:00
Night Shift AM Shift PM Shift Night Shift
Then use a HLOOKUP to return the correct value:
=HLOOKUP(F4,O1:R2,2,TRUE)

I took a slightly different path that Scotts.
A Night Shift occurs if the time is greater or equal to 10PM, OR is less than 6AM.
=OR($F$4<$P$1,$F$4>=$R$1)
An AM Shift occurs when the time is greater or equal to 6AM, AND is less than 2PM.
=AND($F$4>=$P$1,$F$4<$Q$1)
A PM Shift occurs when the time is greater or equal to 2PM, AND is less than 10PM.
=AND($F$4>=$Q$1,$F$4<$R$1)
Stick the three conditions together and you have:
=IF(OR($F$4<$P$1,$F$4>=$R$1),"Night Shift",IF(AND($F$4>=$P$1,$F$4<$Q$1),"AM Shift",IF(AND($F$4>=$Q$1,$F$4<$R$1),"PM Shift","")))
Edit
During testing I entered 00:00:00 in A1 and =A1+TIMEVALUE("00:01:00") in A2:A1440.
At 06:00:00, 14:00:00 and 22:00:00 the changeover in shift happened a minute later.
If, however, I manually typed in 06:00:00 the changeover happened on the hour. This seems to be because TIMEVALUE is calculating 6AM as 0.2499999 rather than 0.25.

=IF(OR(HOUR(NOW())>22,HOUR(NOW())<7),"NIGHT","")&IF(AND(HOUR(NOW())>6,HOUR(NOW())<15),"MORNING","")&IF(AND(HOUR(NOW())>14,HOUR(NOW())<23),"LATE","")
I know a bit late but simplifies everything without the need of using other cells (note there are different times used).
Night - 23-07
AM - 07-15
PM - 15-23

Related

How to check if a specific time falls in between 2 times in Excel?

I used this formula IF(AND(C2>=MIN(A2:B2),C2<=MAX(A2:B2)),"Within Shift","Post Shift"). In the image attached the shift is 5pm to 2am, but the comments comes as post shift. Since Excel consider time as 24 hrs the comment comes out as "Post Shift" is there a way to fix this? Please help.
Your formula is based on the idea that both times are within the same day, which is not true in the last case (the shift end time is on the next day).
In order to solve this, it might be useful to realise that datetime values in Excel are based on the idea that one day is equal to the value 1.
So, in case B2 is smaller than A2, you might need to replace the usage of B2 by the usage of B2+1 (the same time, but the next day), so your formula becomes something like (multiline for readability purposes):
=IF(A2<B2,
<use your formula, based on B2>,
<use your formula, based on B2+1>)
The second part, about using your formula, based on B2+1, this means something like:
IF(AND(C2>=MIN(A2:B2+1),C2<=MAX(A2:B2+1)),
"Within Shift",
"Post Shift")
Possible solution:
=IF(AND(IF(C2>=A2,C2,C2+1)<=IF(B2>=A2, B2, B2+1)), "Within Shift", "Post Shift")
The underlying assumption is that column C is always greater than column A.
Offline time could either be prior the start of shift or after the end of shift. Example: Assuming start of shift 07:00 AM and end of shift 04:00 PM and off time at 02:00 AM. We can either interpret 02:00 AM to be prior to 07:00 AM or to he next day after 04:00 PM. There is no way to solve the ambiguity. The safest bet is to assume the off time is always after the start of shift. As such, since 02:00 AM < 07:00 AM, we add 1 day and compare the result to the end of shift. Same applies to end of shift, if it seems to be prior to start of shift, we add 1 day to it prior to comparison.
Here's a different approach to the existing answers:
=IF(MOD(1+C2-A2,1)=MIN(MOD(1+C2-A2,1), MOD(1+C2-B2,1)), "Within Shift", "Post Shift")
Basically, it takes the "target" time (Shift End or Offline Time), adds 24 hours (1 day), subtracts the Shift Start Time, and fits it inside a day (i.e. if the result is over 24 hours, keep subtracting 24 hours until the time is less than a day)
This will give you either the length of their shift, or how long they were online (depending on which is the "target time").
We then use MIN to find out which of these two times were shorter, and then compare that to the Time Spent Online. If they are the same, then they went offline within their shift. If they are different, then they went offline after their shift.
Note that if they go offline exactly at shift-end, this will be counted as "Within Shift". If you want this to be "Post Shift", then change the MIN to MAX, and swap the two labels

Excel Categorize Times of day

I am trying to create an excel function that categorizes the time of day.
I have a column of DateTimes, Format: 3/3/2017 13:30 (but can change the format if needed). I need a second column declaring "ON" for the hours 9 pm to 5 am, "AM" for 5 am to 9 am, "BH" for 9 am to 5 pm, and "PM" for the hours 5 pm to 9 pm. I need weekends too but I figure I can pull those manually.
Result would look like (where I have column 1 and need to calculate column 2):
DateTime Time period
3/3/2017 13:30 BH
3/3/2017 17:30 PM
3/4/2017 3:30 ON
3/5/2017 5:30 AM
Make a table with the lower time and expected out put:
Then a simple VLOOKUP:
=VLOOKUP(MOD(A2,1),F:G,2)
We can save a few characters by only testing one end of each range. Once we have tested a value for less than 5 am, we no longer need to test to see if it is greater than 5 am. (The AND( statements aren't necessary.)
=IF(HOUR(A1)<5,"ON",IF(HOUR(A1)<9,"AM",IF(HOUR(A1)<17,"BH",IF(HOUR(A1)<21,"PM","ON"))))
If you want to detect weekends, wrap the whole thing in a weekend test. Two methods presented:
The better approach, suggested by #ScottCraner in the comments:
=IF(weekday(a1,2)>5,"WEEKEND","THE WHOLE THING")
The first argument for weekday( is obviously the date we are testing. The second forces Monday to be the first day of the week, which leaves Sat & Sun as the last two.
Combined with the rest, we would get:
=IF(weekday(a1,2)>5,"WEEKEND",IF(HOUR(A1)<5,"ON",IF(HOUR(A1)<9,"AM",IF(HOUR(A1)<17,"BH",IF(HOUR(A1)<21,"PM","ON")))))
An unnecessarily long, and somewhat fragile approach (breaks in non-English)
=IF(LEFT(TEXT(A1,"ddd"),1)="S","WEEKEND","THE WHOLE THING")
This works because TEXT(A1,"ddd") formats the date as the three-letter day of week. In English, at least, both weekend days starts with an "S", and we use left( to grab that first letter.
Together, it would end up looking like:
=IF(LEFT(TEXT(A1,"ddd"),1)="S","WEEKEND",IF(HOUR(A1)<5,"ON",IF(HOUR(A1)<9,"AM",IF(HOUR(A1)<17,"BH",IF(HOUR(A1)<21,"PM","ON")))))
Just to show a different way:
=IF(NETWORKDAYS(A2,A2),CHOOSE(SUM((({"5:00";"9:00";"17:00";"21:00"}*1)<MOD(A2,1))*1)+1,"ON","AM","BH","PM","ON"),"WEEKEND")
This is an array formula and must be confirmed with ctrl+shift+enter.
Can be extended as you like without getting in trouble of the bracket-limit.
Also like the Scott's answer: this works with times like 17:43. ;)
A little long, but works:
=IF(AND(HOUR(A2)>=9,HOUR(A2)<17),"BH",IF(AND(HOUR(A2)>=17,HOUR(A2)<21),"PM",IF(AND(HOUR(A2)>=5,HOUR(A2)<9),"AM","ON")))

calculating overtime hours in excel

I'm trying to make spreadsheet that calculate overtime hours as the following:
If the task started after 2pm the hour is calculated as an hour and a half
if the task started after 12 am the hour is calculated as 2 hours
I wrote the following formula in column E:
=IF(AND(B5>G1;B5<G2);D5*D1;D5*D2)
but I get false results where did I went wrong?
thank you
The problem with your formula is that you are thinking of 12:00 AM as midnight. or 24:00. If you look at G2 you will see 12:00 AM or (24:00) in Excel it is stored as 0. You logical check is checking to if anything is less than zero. Assuming that you are never dealing with negative time your result will always be false as no time is less than zero.
I would change your layout a bit and add a shift 3 where there is no overtime. What happens when someone starts at 9 AM?
IF you have to keep your layout as is, I would change your formula in cell D5 to read:
=HOUR(C5-B5)
And then make sure cells D1,D2,D5 and E5 are all general format since they are just numbers and not actual time.
Next set G2 equal to 08:00
change your formula in E5 to:
=IF(B5<G2,D5*D2,IF(B5>=G1,D5*D1,D5))
or
=D5*IF(B5<G2,D2,IF(B5>=G1,D1,1))
See if that gets you any further.
One thing to watch out for is that Excel stores time in a decimal format which can really throw you off. You may think your are dealing with 12 when you see 12:00 PM in a cell, but what excel really has stored in that cell is 0.5 as it is half way through the day. 24:00 does not exist Excel formulas will treat it as 0 if it is supplied as an input for formulas. VBA will not accept it. Valid Excel times range from 00:00 to 23:59.
now having said this if you are looking strictly at 8 hour shifts and are only concerned about start times, you could simply do an if statement saying IF(start time = shift 1 start time, then multiply by shift 1 rate, IF( start time = shift 2 start time, then multiply by shift 2 rate, no special rate))
something else to consider, if you are looking at actual start times, and a person starts in one shift but finishes during the next shift what happens?

Formula to get the worked hours from time A to time B where B is past midnight

I'm using Excel to write down my shifts and get a total of worked hours per day and per week.
I structured it this way:
Everything seemed to work fine until I finished to work at 12.30 am. The result with my formula was -17 hours instead of 7. How can I fix my formula so that it displays a correct amount? I'm using the following formula and I want the result to be displayed in number format, not time.
=IF(C11=0,0,IFERROR(((C11-B11)-D11)*24,0))
What formula can I use?
Excel treats days as 1 for every day past Dec 31, 1899. Today happens to be 42,217. Time is nothing more than a decimal portion of a day. Today at noon was 42,217.5 and tomorrow at 03:00 will be 42,218.125.
Excel also treats boolean (e.g. TRUE/FALSE) values as either 1 or 0 when used in a mathematical equation. e.g. 0.5 + TRUE = 1.5 while 0.5 + FALSE = 0.5.
Test to see if the minuend is less than the subtrahend and if it is, add 1 to it using the result of the test itself.
=(C11+(C11<B11)-B11)*24
      
Finally, it should be mentioned that while you can subtract a larger time from a smaller time to receive a negative decimal value, the negative value cannot be interpreted as time since Excel does not recognize negative time. If you were not multiplying by 24 to retrieve the hours as integers and simply subtracting B11 from C11 the cell would be filled with hashmarks (e.g. ############) to show the error. e.g. 08:00 - 10:00 = (as time) #######.

Excel how get number of hours in a time interval?

I have 2 columns with:
Night shift start: 19:00
Night end: 04:00
And I have some date columns with for each day..
Work started: 07:30
Worked ended: 22:00
I want to get the number of hours as a decimal that is between the night shift start and night end. I need to calculate the number of "night shift hours" for worked hours.
From comment: I do not want to get the total number of hours. I want to calculate the number of "night shift hours" and that is hours between 19:00-04:00
=IF(B1-A1 < 0, 1-(A1-B1),( B1-A1))
Assuming that cell A1 contains start, B1 contains end time.
Let me know, if it helps OR errors.
Time without date is not enough to do the subtraction considering the start can be the night before today.
Are you OK to try VBA?
EDIT: The formula is meaningful within 12 hour limit. I will see if it can be made simpler.
Given start time in B5 and end time in C5 this formula will give you the decimal number of hours that fall in the range 19:00 to 04:00
=MOD(C5-B5,1)*24-(C5<B5)*(19-4)-MEDIAN(C5*24,4,19)+MEDIAN(B5*24,4,19)
format result cell as number
just substract the two dates to get the difference in days, and multiply by 24 to get the difference in hours
=(B1-A1)*24
this is correct when both B1 and A1 contain a datetime value, but in case your cells contain just a time value, with no day value, and given that the calculation spans the night (there is a day change in between) you need to add one day to the difference:
=IF(B1<A1,1+B1-A1,B1-A1)*24

Resources