Check if the in and out time falls between a specific time interval

srivibish

New Member
Joined
Feb 20, 2017
Messages
4
I am looking for a solution after a long trial and error workings. I have 2 columns in and out with date and time. I want to check if the in and out falls between the interval 10PM to 8AM of next day. Further, for those which falls within this range, I want to check atleast minimum of 3 hours are falling within this interval. My data looks like below in col A and B.
I need the output in column C and D where column C gives me the in and out time falls between the interval and Col D give me if it falls more than 3 hours between 10PM to 8AM.

InOutBetween 10-8<=3hrs
09/24/2019 06:00:1809/25/2019 01:00:22YesYes
09/09/2019 17:54:5809/10/2019 03:32:03YesYes
09/03/2019 10:19:4909/03/2019 23:22:40YesNo
10/01/2019 00:01:2210/01/2019 11:49:39YesYes
09/24/2019 23:59:4609/25/2019 09:10:38YesYes
09/09/2019 13:11:4109/10/2019 01:36:47YesYes
09/09/2019 13:11:4109/09/2019 21:36:47NoNo
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!


Cross posted at: Check if the in and out time falls between a specific time interval
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
For an example - how does 10/01/2019 00:01:22 to 10/01/2019 11:49:39 (Data row 4) fall within 10PM and 8AM of the next day?
 
Upvote 0
The entire IN and Out need not to be falls within the interval. If you see the row 4 data, the in time is early morning 1AM and out time is 8AM which falls between the 10PM to 8AM interval. Hence, I have captured as Yes for that
 
Upvote 0
Sorry, the out is 11.49. My Miss.

"The entire IN and Out need not to be falls within the interval. If you see the row 4 data, the in time is early morning 1AM and out time is around 11AM which falls between the 10PM to 8AM interval. Hence, I have captured as Yes for that

10/01/2019 00:01:2210/01/2019 11:49:39YesYes
Are you referring to the same set of data as you have posted?
This is what I am referring
 
Upvote 0
We have a time worked, given by a "Time In" and a Time Out". Those two times are recorded to a precision of 1 second, and they occur on either the same day or sequential days, so a date stamp and a time stamp comprise the Time In and the Time Out. I am assuming that in no case can someone work more than 24 hours, and that the time worked is continuous, i.e., a single worked time block. We want to determine at least two things: 1) Does any portion of the time worked lie within a time block of 10 PM to 8 AM...akin to a night shift? 2) If so, then did the amount of time worked during this time block equal or exceed 3 hours?

Rather than trying to deal with times on different days, I propose establishing a 2-day window beginning at midnight of Day 1 (i.e., the day associated with Time In) and ending just before midnight of Day 2 (the day after the day associated with Time In). By doing this, we can map the Time In and the Time Out to this window. We can also conveniently map 10 PM and 8 AM to this same window. This facilitates visualizing the problem and constructing formulas to answer the questions. Upon sketching the timeline, we see some issues: the time block of interest spans two days, from midnight of Day 1 through 8 AM of Day 1, from 10 PM of Day 1 through 8 AM of Day 2, and from 10 PM of Day 2 until midnight leading into Day 3, labeled as blocks A, B, and C, respectively. It may be conceivable for someone to work long hours such that they have time in more than one of these time blocks.

Excel defaults to expressing Dates in terms of days and fractional days, so I maintain this convention and treat the window as running from [0,2]. Then the night shift time blocks map as (0,0.333) d, (0.917,1.333) d, and (1.917,2) d. I did not automate this part of the worksheet. The user needs to manually break up any time blocks to fill the entire 2-day window, and then create a table of start and end times for each of the blocks, paying special attention to compute these end points using formulas, e.g., 10 PM on Day 2 is =1+22/24, while 10 PM on Day 1 is =22/24. Similarly, 8 AM on Day 1 is =8/24, and 8 AM on Day 2 is =1+8/24.

The beginning of Day 1 (midnight leading into Day 1) is readily determined using the DATE function applied to the worked Time In, and this establishes time "0". Similary, decimal days relative to time 0 are found via subtraction (actual date&time - beginning of Day 1) for Time In (I) and Time Out (O). Then we examine each of the A,B,C time blocks to determine hours worked during those time blocks. Focusing first on Block B---the central one on the timeline---we have a number of possibilities shown below as 1B-7B. Technically, 6B reverts to 1B because our definition of Day 1 is the day when Time In is recorded. All of these feature Time In's that begin after Block A on Day 1, and the five scenarios address every possibility where a Time Out could occur, with the provision that no Time Out occurs within Block C (this will be addressed later).

The spreadsheet formulas were developed and confirmed against contrived Time In's and Time Out's that satisfied scenarios 1B-7B.

timeblocks_png.png


Then the same formula is applied to the other two time blocks to determine the hours worked in each. Now, I am not certain what to do with these results, as the original question is somewhat unclear. Is the question whether 3 or more hours are being worked only during the central time block...block B? Or is the question whether the sum of any hours worked within any of these blocks during a single day >= 3 h. The implication of the latter question being that a Time In of 7 AM and a Time Out of 11 PM gives 2 h total within the night shift time blocks...1 hour in Block A and 1 hour in Block B. The solution presented here gives this breakdown.

One note, this approach will fail under scenarios 9A and 8B because those have Time Outs that extend beyond the upper map limit of 2 days. This approach could be easily extended to include an extra 12 or 24 hours to correctly calculate these situations, but I do not know if it is necessary...or feasible or likely that someone would work for more than 24 hours straight.

I encountered one issue with this approach and wanted to make others aware of it, hoping that someone has ideas for addressing it. If I take one of the examples having a Time In of 9/9/2019 13:11:41 and a Time Out of 9/9/2019 21:36:47, the 4-part nature of the equation to calculate the amount of time worked within a time block appears to be vulnerable to computational precision errors. Rather than return 0, it returns a "days worked" value of -5.55111512312578E-17. Evaluation of the formula confirms that it performs the intended computations, but the expected value is 0. While the computed result is, for all intents and purposes, 0, this causes a problem for any logical of IF statements checking for whether the value is truly 0, as might be the case if one wanted to know whether any time was worked during a block. As a work-around, I would set a tolerance (TOL) of 0.001 s, converted to hours, and then check whether and very near 0 value is smaller than TOL, and if so, set it to 0.

Columns Q:Z were left here to facilitate investigating this issue, if there is any interest. One can see indications of the problem when looking at my summary of total time worked (from the difference...Time Out - Time In). When parsing this value, originally expressed in days, into hh:mm:ss, the seconds (see Column S) reveal this computational issue. The difference between two times recorded to a precision of 1 second should result in a difference whose precision is also 1 second.

MrExcel_20200325.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1Block ->ABCTOL (s) ->0.001
2Start12:00 AM10:00 PM10:00 PMTOL (h)######
3End8:00 AM8:00 AM12:00 AM
4Map [0,2)
5Start0.0000.9171.917
6End0.3331.33333332.000
7
8ABCABCABC
9ScenarioInOutBetween 10P-8A<=3hrsBeginning of Day InDecimal "Time In" (ref)Decimal "Time Out" (ref)Hrs between LimitsHrs between LimitsHrs between LimitsTotal time worked in 10P-8A blocks (h)>= 3 h worked, combined all blocks?>= 3 h worked in only one block?Total Hrs workedhhmmss=0=0=0~0~0~0
10orig 19/24/2019 6:00:189/25/2019 1:00:22YesYes437320.250211.041921.995003.006110.000005.00111TRUETRUE19.0011904.000000097FALSEFALSETRUEFALSEFALSETRUE
11orig 29/9/2019 17:54:589/10/2019 3:32:03YesYes437170.746501.147260.000005.534170.000005.53417TRUETRUE9.6189375.000000233FALSEFALSETRUETRUEFALSETRUE
12orig 39/3/2019 10:19:499/3/2019 23:22:40YesNo437110.430430.974070.000001.377780.000001.37778FALSEFALSE13.04813251.00000016TRUEFALSETRUETRUEFALSETRUE
13orig 410/1/2019 0:01:2210/1/2019 11:49:39YesYes437390.000950.492817.977220.000000.000007.97722TRUETRUE11.805114816.99999974FALSETRUETRUEFALSETRUETRUE
14orig 59/24/2019 23:59:469/25/2019 9:10:38YesYes437320.999841.382380.000008.003890.000008.00389TRUETRUE9.18191052.00000028TRUEFALSETRUETRUEFALSETRUE
15orig 69/9/2019 13:11:419/10/2019 1:36:47YesYes437170.549781.067210.000003.613060.000003.61306TRUETRUE12.41812256.000000006TRUEFALSETRUETRUEFALSETRUE
16orig 79/9/2019 13:11:419/9/2019 21:36:47NoNo437170.549780.900540.000000.000000.000000.00000FALSEFALSE8.4188255.999999586FALSETRUETRUETRUETRUETRUE
170.000
181B/6B9/9/2019 9:30:009/9/2019 20:30:00NoNo437170.395830.854170.000000.000000.000000.00000FALSEFALSE11.000105959.99999958FALSETRUETRUETRUETRUETRUE
192B9/9/2019 9:30:009/10/2019 2:45:00NoNo437170.395831.114580.000004.750000.000004.75000TRUETRUE17.25017150TRUEFALSETRUETRUEFALSETRUE
203B9/9/2019 9:30:009/10/2019 9:45:00NoNo437170.395831.406250.0000010.000000.0000010.00000TRUETRUE24.250241459.99999979TRUEFALSETRUETRUEFALSETRUE
214B9/9/2019 23:00:009/10/2019 4:30:00YesYes437170.958331.187500.000005.500000.000005.50000TRUETRUE5.50052959.99999979TRUEFALSETRUETRUEFALSETRUE
225B9/9/2019 23:00:009/10/2019 10:30:00NoNo437170.958331.437500.000009.000000.000009.00000TRUETRUE11.500112959.99999979TRUEFALSETRUETRUEFALSETRUE
237B9/9/2019 9:30:009/10/2019 23:30:00NoNo437170.395831.979170.0000010.000001.5000011.50000TRUETRUE38.000375959.99999958TRUEFALSEFALSETRUEFALSEFALSE
240.000
254A9/9/2019 0:00:009/9/2019 6:01:00NoNo437170.000000.250696.016670.000000.000006.01667TRUETRUE6.017611.95578E-07FALSETRUETRUEFALSETRUETRUE
265A9/9/2019 1:30:009/9/2019 9:45:00NoNo437170.062500.406256.500000.000000.000006.50000TRUETRUE8.2508150FALSETRUETRUEFALSETRUETRUE
276A9/9/2019 4:30:009/10/2019 0:00:00NoNo437170.187501.000003.500002.000000.000005.50000TRUETRUE19.50019300FALSEFALSETRUEFALSEFALSETRUE
286A9/9/2019 4:30:009/10/2019 4:30:00YesYes437170.187501.187503.500006.500000.0000010.00000TRUETRUE24.0002400FALSEFALSETRUEFALSEFALSETRUE
297A9/9/2019 4:30:009/10/2019 9:30:00NoNo437170.187501.395833.5000010.000000.0000013.50000TRUETRUE29.0002902.09548E-07FALSEFALSETRUEFALSEFALSETRUE
308A9/9/2019 7:00:009/10/2019 23:30:00NoNo437170.291671.979171.0000010.000001.5000012.50000TRUETRUE40.50040300FALSEFALSEFALSEFALSEFALSEFALSE
31
329A9/9/2019 7:00:009/11/2019 0:30:00NoNo437170.291672.020831.0000010.000002.0000013.00000TRUETRUE41.50041304.19095E-07FALSEFALSEFALSEFALSEFALSEFALSE
338B9/9/2019 9:30:009/11/2019 0:30:00NoNo437170.395832.020830.0000010.000002.0000012.00000TRUETRUE39.0003900TRUEFALSEFALSETRUEFALSEFALSE
Sheet7
Cell Formulas
RangeFormula
W2W2=W1/60/60
K5K5=22/24
L5L5=1+22/24
J6J6=8/24
K6K6=1+8/24
L6L6=2
G32:G33,G25:G30,G18:G23,G10:G16G10=DATE(YEAR(B10),MONTH(B10),DAY(B10))
H32:H33,H25:H30,H18:H23,H10:H16H10=$B10-$G10
I32:I33,I25:I30,I18:I23,I10:I16I10=$C10-$G10
J32:L33,J25:L30,J18:L23,J10:L16J10=((IF($I10>J$5,$I10-J$5,0)-IF($H10>J$5,$H10-J$5,0))-(IF($I10>J$6,$I10-J$6,0)-IF($H10>J$6,$H10-J$6,0)))*24
M32:M33,M25:M30,M18:M23,M10:M16M10=SUM(J10:L10)
N32:N33,N25:N30,N18:N23,N10:N16N10=SUM(J10:L10)>=3
O32:O33,O25:O30,O18:O23,O10:O16O10=OR(J10>=3,K10>=3,L10>=3)
Q32:Q33,Q25:Q30,Q18:Q23,Q10:Q16Q10=INT(P10)
R32:R33,R25:R30,R18:R23,R10:R16R10=INT((P10-Q10)*60)
S32:S33,S25:S30,S18:S23,S10:S16S10=(P10-Q10-R10/60)*3600
U32:W33,U25:W30,U18:W23,U10:W16U10=J10=0
X32:Z33,X25:Z30,X18:Z23,X10:Z16X10=IF(ABS(J10)<=$W$2,"TRUE","FALSE")
P32:P33,P10:P30P10=(I10-H10)*24
Cells with Conditional Formatting
CellConditionCell FormatStop If True
X33:Z33Expression=NOT(EXACT(U33,X33))textNO
X23:Z23Expression=NOT(EXACT(U23,X23))textNO
X32:Z32Expression=NOT(EXACT(U32,X32))textNO
X30:Z30Expression=NOT(EXACT(U30,X30))textNO
X31:Z31,X34:Z34,X24:Z29,X10:Z22Expression=NOT(EXACT(U10,X10))textNO
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top