How do I calculate this time? Across midnight - excel

I have a sheet with start and end dates and values.
In C I have start date.
In E I have end date.
In I I have start time. (06:05:00)
In J I have end time. (08:33:00)
If C <>E I need to add 24 hours to the time elapsed.
How can I do that?
I tried if(C2<>E2;1+J2-I2;"omitted")
But I get the result 21:32:00.
It should be about 26 hours
(24+8-6 = 26 if we only look at the hours).
What have I done wrong?
Edit;
Back at work and can now upload some images.
Method 1
Method 2
Both return the wrong time.
EDIT2;
Method 3
I remember how I always have to format the dates from "our" format to Excels format for it to be recognized as a date.
In column P I use RIGHT(), MID(), and LEFT() to make a correct formatted date.
In R and S I use the same as P&Q column.
Still not correct.
:-/
EDIT again:

Use formula: =(E1+J1)-(C1+I1) we just add the date and time together and subtract the end from start and then format the cell having the formula to show days, hours, minutes.
This way, if you have more than 1 day difference, you're not just adding 24 hours.
Change the format of the target cell contianing the above formula to
d "days" hh "hours" mm "minutes"
or use the format tigerAvatar suggested of [hh]:mm if you want the hours to be cumulative across days.
Then you get a nice output of: 2 days 02 hours 28 minutes or 50:28
Feel free to drop the 1 h or 1 m if you don't want the leading zeros.
A picture is worth a 1000 words so:
Version 2 after your screenshots: I don't think C, E are in date format based on your updates so...
I used formula=(DATE(LEFT(E2,4),MID(E2,5,2),RIGHT(E2,2))+J2)-(DATE(LEFT(C2,4),MID(C2,5,2),RIGHT(C2,2))+I2) in K and custom format mask: [h]:mm
If this doesn't work it may be a regional setting and the interpretation of [h]:mm I am assuming I/J are time formats.

Related

Excel hours worked calculation, based on specific time shifts

I have an excel with working hours in format Starting: 15:30 (A1) and finishing: 01:00 (B1). I would like to calculate the hours that i worked as nightshift (C1). The night shift is between 22:00 and 06:00. So if i work for example 17:00 to 02:15, i should get the number 04:15 as nightahift hours. I i work from 22:00 to 09.15 i should get 8 nightshift hours. I got the time difference with this
""=MOD(B2-A2;1)"" but this gets the whole shift and not the night ones only. Any suggestions?
It seems that this code works for one pair of columns, but i cannot figure out how to sum the results. The excel is seperated into columns, where every date has 2 columns for Starting and Ending shift
=MOD(D4-C4;1)24-(D4<C4)(22-6)-MEDIAN(D424;6;22)+MEDIAN(C424;6;22)
The ideal would be to have a formula to this result
=MOD(D4-C4;1)24-(D4<C4)(22-6)-MEDIAN(D424;6;22)+MEDIAN(C424;6;22)+
MOD(F4-E4;1)24-(F4<E4)(22-6)-MEDIAN(F424;6;22)+MEDIAN(E424;6;22)+
MOD(H4-G4;1)24-(H4<G4)(22-6)-MEDIAN(H424;6;22)+MEDIAN(G424;6;22) etc, but more compact way
Assuming that all input times are relative to 1/0/1900 (only time portion information). Therefore you need to consider when the date refers to the next day (adding 1). The condition for the end date is that if start > end, then the end date refers to the next day (+1). We take into account the above for the solution.
Assuming no excel version constraints as per the tags listed in the question, you can use the following formula in F2:
=LET(start, A2:A5, end, B2:B5, ns, D2, ne, D3+1, eEff, IF(end<start, end+1, end),
CALC, LAMBDA(isStart, MAP(start, eEff, LAMBDA(x,y, IF(y < ns, 0,
MEDIAN(IF(isStart, x, y), ns, ne))))), a, CALC(TRUE), b, CALC(FALSE),b-a)
I created a user LAMBDA function CALC just to avoid repetition of a similar calculation for the actual start (a) and end (b) of the working night interval. As you can see from the formula the night end (ne) has been adjusted to consider the next day. Similarly, we do the adjustment for the end time, calculating the effective end time (eEff) considering the next day when the end time (end) is lower than the start time (start).
We use MEDIAN to identify the actual interval in both cases. Here is the output:
where 0:00 means no time consumed during the night shift. The rest is just to use the correct format: hh:mm in the output to show durations during the night shift.
If you show the input data in numeric format, you can see the times referred to the first valid year in Excel (<= 1):
That is why we need to convert ne and end (in case it represents the next day), but it depends on whether or not you want to consider the next day in your input data. So for row 2 it represents 01:00 of the current day (0.04) so in the formula, we treat it as: 01:00 of the next day (1.04)

Excel formula to calculate elapsed minutes between 2 date timestamps only counting minutes during work hours

I have an excel sheet with about 50,000 records where I need to find the number of minutes between two date timestamps but I need to exclude any minutes that occurred during the times we are not working.
Our schedule is M-F 8:30am-5:30pm, Saturdays 8:30am-1:30pm
We don't work Sundays or holidays.
As an example
Cell B2: [7/3/2020 2:16:21 PM]
Cell C2: [7/6/2020 9:20:23 AM]
The manually calculated answer for this one should be about 244 minutes. Task started Friday afternoon, Saturday was a holiday, don't work Sundays, task completed at 9:20am on Monday.
Usually, I come here and start writing a question and by the time I've understood my own problem well enough to post a question I have figured it out on my own but not this time! Help!
Update:
#ForwardEd shared this...
=((I2-H2)
-MAX(0,(NETWORKDAYS.INTL(H2,I2,"0000011",$M$2:$M$12)-1+(WEEKDAY(I2,1)=7)))*TIME(15,0,0)
-MAX(0,(NETWORKDAYS.INTL(H2,I2,"1111101",$M$2:$M$12)-(WEEKDAY(I2,1)=7)))*TIME(19,0,0)
-NETWORKDAYS.INTL(H2,I2,"1111110",$M$2:$M$12)-(NETWORKDAYS.INTL(H2,I2,"0000000")
-NETWORKDAYS.INTL(H2,I2,"0000000",$M$2:$M$12)))*24*60
Where H:H is the Start Date Timestamp and I:I is the Response Date Timestamp and M2:M12 contains my holiday list.
It worked beautifully until I ran into an example like this:
H2 - 07/26/2020 7:48:45 PM
I2 - 07/27/2020 8:57:58 AM
The net result was -650.78333. It looks like anything that starts one one day and ends on the next is coming back as negative.
We want to measure the average response time in minutes for the applications that require manual underwriting. These start timestamps are times that loan applications were received online so they could come in any time of day. The stop times are timestamps that represent the system recorded response time. i.e. the timestamp where an underwriter first did something with the loan application. If a loan application was received at 7pm and was not auto-decisioned then a manual underwriter will need to do something with it the next day when we start working.
If that application came in at 7pm on Wed and is decisioned by an underwriter at 8:46am on Tuursday, we would want to document 16 minutes for that application - not 826 counting the hours between 7pm and 8:30am.
What you want to look at is NETWORKDAYS.INTL. Use this in conjunction with the custom settings to determine the number of Saturdays, Sundays and for the number of days in between your start and end time. You know you have X amount of time per day that is non working time, and Y amount per Saturday.
Then you formula in essence becomes
(End time - start time) - X * No. Weekdays - Y * No. Saturdays - No. Sundays - No. Holidays
Now there will be some tricks in there in order to count your days. but that is the gist of what it boils down to in a formula.
The formulas that are doing the brunt of the work are:
WORKDAY
NETWORKDAYS.INTL
TIME
I avoided the use of an if statement by using a boolean operation that excel will resolve from TRUE/FALSE to 1/0 when sent through a math operator. Side note: I read somewhere that this is also faster than an IF statement, but have no way of proving it and really does not matter on a small number of calculations.
WORKDAY
This formula will return the day of the week for a given date, and a set day of the week to be 1. It will be need in this solution to determine if the end date is a Saturday which has a value of 7 in default setup up as well when option 1 is picked. The format for the formula is:
WORKDAY(Excel Serial date, day 1 of the week)
For this solution
WEEKDAY(B3,1)
NETWORKDAYS.INTL
This formula will be used to count the number of specific days a start and an end date. It can exclude a custom weekend or count a custom week. If it is supplied with a list of dates that are holidays they can be excluded as well. The basic format of the formula is:
NETWORKDAYS.INTL(Start Date, End Date, Custom week choice or workweek pattern, range of holiday dates)
When entering the formula it will give you a list of predefined options for the weekend choices. It will not talk about the pattern.
The pattern is a string 7 digits long consisting of 1 or 0. 0's represents the days you want to count and 1's are days you want to ignore. An important part of the pattern is that the first entry is MONDAY. "1010111" would count only Tuesdays and Thursdays.
TIME
Excel stores date as an integer. 1 represents 1st of January 1900, 2 the 2nd of January 1900 and so on. Time is stored as a decimal or if you prefer the percentage/fraction of a day or 24 hour period. So rather than figuring out the math to determine what percentage of a day X number of hours is, it is simpler to let excel calculate it for us and make the number a little more understandable to someone who may be deciphering the formula later. The basic format of the formula is:
TIME(Hours, Minutes, Seconds)
So as stated earlier, 6 key components need to be determined:
X - Amount of non working time after a weekday
Y - Amount of non working time after a Saturday
Number of weekdays
Number of Saturdays
Number of Sundays
Number of holidays
1) Determine Weekday Non-Working Hours
Based on the supplied information that work day stops at 1730 and starts as 0830. There are a couple of ways of doing the math. Subtract the working hours from 24 hours or count the non work hours at the end of the day and add them to the non work hours at the start of the day.
24 - (17.5 - 8.5) = 15
or
(24 - 17.5) + (8.5 - 0) = 15
For this example 15 will be hard coded into the final formula
2) Determine Saturday Non-Working Hours
Similar to above. Note that we are ignoring Sunday as it is a designated non working day which we already know is 24 hours or 1 day. We are just interested in the time between end of shift Saturday and start of the next normal working Monday. So it really gets calculated the same with just with difference end of shift time.
24 - (13.5 - 8.5) = 19
or
(24- 13.5) - (8.5 - 0) = 19
For this example 19 will be hard coded into the final formula
3) Determine Number of Weekdays
Based on the description earlier of of NETWORKDAYS.INT and working with the assumption that holidays are stored in the range F2:F2, and using a pattern of "0000011" the number of weekdays the formula will be as follows:
=NETWORKDAYS.INTL(B2,B3,"0000011",F2)
For this example the formula is place in cell F6
4) Determine Number of Saturdays
Similar 3) adjust the pattern to only select Saturdays by using "1111101"
=NETWORKDAYS.INTL(B2,B3,"1111101",F2)
For this example the formula is place in cell F7
5) Determine Number of Sundays
Similar 4) adjust the pattern to only select Saturdays by using "1111110"
=NETWORKDAYS.INTL(B2,B3,"1111110",F2)
For this example the formula is place in cell F8
6) Determine Number of Holidays
To get the number of holidays there is not a direct way of doing it. Instead take the difference between all days counted without holidays being factored in and all days counted with holidays counted in.
=NETWORKDAYS.INTL(B2,B3,"0000000")-NETWORKDAYS.INTL(B2,B3,"0000000",$F$2:F2)
For this example the formula is place in cell F9
Now at this point I would love to say just substitute all of the above into the generic formula, but there are a couple of special cases that need to be taken care of. You may have also noted I have not used the WEEKDAY formula yet.
So in order to count the number of days to which X is going to apply, its really the number of days minus 1. The minus 1 is because you want to cont the intervals between days, not the number of days themselves. This gets a little bit more trickier when the end day is a Saturday because there is still an interval there but Saturday is not counted as a weekday. So the True count for number of weekday intervals is:
=MAX(0,(F6-1+(WEEKDAY(B3,1)=7)))
I originally had the MAX(0, calc) in there to prevent the posibility of the day count being negative. After arriving at this final format it may not be needed and you might get away with the following but its untested:
=F6-1+(WEEKDAY(B3,1)=7)
This same concept needs to be applied to your Saturday count. If you job ends on Saturday you do not need to subtract the non working hours after the last Saturday. You formula will look like:
=MAX(0,(F7-(WEEKDAY(B3,1)=7)))
and again further testing is required to make sure MAX can be removed, but if it can then the formula would look like:
=F7-(WEEKDAY(B3,1)=7)
So now with the understanding how dates and times are stored, determine the time difference between start end end time and subtract all the non working hours.
=(B3-B2)-MAX(0,(F6-1+(WEEKDAY(B3,1)=7)))*TIME(15,0,0)-MAX(0,(F7-(WEEKDAY(B3,1)=7)))*TIME(19,0,0)-F8-F9
Now you will not want to use helper cells, so you can take each of the individual formula from F6 to F9 and wind up with:
=(B3-B2)-MAX(0,(NETWORKDAYS.INTL(B2,B3,"0000011",F2)-1+(WEEKDAY(B3,1)=7)))*TIME(15,0,0)-MAX(0,(NETWORKDAYS.INTL(B2,B3,"1111101",F2)-(WEEKDAY(B3,1)=7)))*TIME(19,0,0)-NETWORKDAYS.INTL(B2,B3,"1111110",F2)-(NETWORKDAYS.INTL(B2,B3,"0000000")-NETWORKDAYS.INTL(B2,B3,"0000000",$F$2:F2))
The formula looks unruly, but is easier to understand when broken down into its parts.
Now the last step is to get the answer to display in minutes. There are two choices.
You can leave it as it is in an excel serial date format and change the formatting of to a custom format of [m]. The [ ] will force it into minutes and prevent spill over to hours. It will also round to the nearest minute.
You can convert the results to minutes by multiplying by 24*60 and the value will be in minutes and decimal of minutes.
Note that:
A11 has Time formatting applied
A12 has General formatting applied
A14 has custom formatting of [m] applied
It should be something like this:
Create a calendar table with the workinghours for each days in the year you have data in
Date | StartTime | End time
1/1/2020 1/1/2020 8:30:00 PM 1/1/2020 5:30:00 PM
...
7/3/2020 7/3/2020 8:30:00 PM 7/6/2020 5:30:00 PM
...
12/31/2020
Then paste this code in a module
Function CalcDays(dStart As Date, dEnd As Date, daysCalendar As Range)
Dim Cell As Range
Dim MinDaysCalendar As Date, MaxDaysCalendar As Date
Dim aWSF As WorksheetFunction
Set aWSF = Application.WorksheetFunction
'check the minimum en the maximum date in the calendar
With aWSF
MinDaysCalendar = .Min(daysCalendar)
MaxDaysCalendar = .Max(daysCalendar)
End With
'if the date you check is not in the calendar, exit the function
If dStart < MinDaysCalendar Or dStart > MaxDaysCalendar Then
MsgBox "Date not in calendar"
Exit Function
End If
If dEnd < MinDaysCalendar Or dEnd > MaxDaysCalendar Then
MsgBox "date not in calendar"
Exit Function
End If
'sum the time of all the dates between the start and the end
'pick min and max in order to start and stop at the right time per day
Dim tempTime As Integer
With daysCalendar
For i = 1 To .Rows.Count
If .Cells(i, 2).Value >= CLng(dStart) And .Cells(i, 3).Value <= CLng(dEnd) Then
daytime = aWSF.Max(.Cells(i, 2).Value, dStart) - aWSF.Min(.Cells(i, 3).Value, dEnd)
End If
tempTime = tempTime + daytime
Next i
End With
'return the total time
CalcDays = tempTime
End Function
You can call the function by typing =calcdays in a cell and then give the startDay, endDay and calendar column as parameters.
There might still be some flaws in this code but I think we can manage those.

Excel 2013 Date time difference

I have a column with date and time for start (IN) and end (OUT) and I want to calculate the difference in days.
Date: IN Time: IN Date: OUT Time: OUT
24/07/2018 12:15:00 26/07/2018 06:11:00
I combine them into date time IN and OUT by using:
E1=TEXT(A2,"dd/mm/yy ")&TEXT(B2,"hh:mm:ss")
F1=TEXT(C2,"dd/mm/yy ")&TEXT(D2,"hh:mm:ss")
But I am unable to find any working code for the difference in number of hours.
TEXT will convert your dates and times into text (strangely enough).
Adding the date and time will return a date/time value so you could use:
=SUM(C2:D2)-SUM(A2:B2) to return 1.74722222
(edit: if you then give the cell a custom number format of d:h:mm it will display 1:17:56 or 1 day, 17 hours, 56 minutes).
(EDIT AGAIN: Sorry, only just seen you want it in hours. Give the cell a custom number format of [hh]:mm. The square brackets tells it to count over 24 hours. This will return 41:56).
or
=DATEDIF(SUM(A2:B2),SUM(C2:D2),"d") to return 2.
or if you just want days from the dates you could just use
=C2-A2 to return 2.
Dates and times in Excel are just numbers - no need to complicate things by turning them to text.
= (F1 - E1) * 24
Rather easly :)

Adding/Subtracting Whole numbers from Time

I have tried every which way to format cells to subtract the result from time for instance the formula in the cell = 11(this is 11 minutes) I want to take that result minus 8:00:00 to give me 7:49:00 but it doesn't work the result is ####### no matter how big I make the cell. And if I format the cells with the formula to custom [m]:ss then the value changes.
Sample of the Worksheet:
I want Y2 = X3-W3 in a time format.
So, if A1=11
Then in some other cell, (B1 in this example): =TIME(,A1,)
Then subtract from the cell with 8:00:00. (If it's C1...:)
=C1-B1
That will give you the time you want.
Info: The main thing is that you have to tell Excel that your cell with the "11" in it, is minutes. By using the =TIME(,A1,) you will get the value of: 12:11 am. (If you keep it in Date format.) 12:11 am could also be viewed as: 0 Hours, 11 minutes, 0 seconds. And now that it knows, you should be able to subtract.
Try this:
=TIME(HOUR(X3),MINUTE(X3)-W3,SECOND(X3))
The ######### is because you have a negative time. Becuase Excel reads time as decimals of 24 hours, 8:00:00 is .3333 and if you subtract 11 from that you get -10.6666 and date/time can not be negative.

Excel 2013: Convert Hours and Minutes from hh:mm:ss formatted cell in decimal number

I use Excel 2013 and have a cell with the following value:
"08.01.1900 1:45:00"
that will be displayed inside the cell as: "193:45:00" - meaning 193 hours, 45 minutes and 00 seconds (see the first image below).
Here is screen of how it looks like and the formatting of the cell
[how it looks like]
QUESTION: How I can in cell "Salary" to get decimal number, like - 193,45? Help me, please.
The cell is formatted as Time (see the image below)
So, you have Q2 = 12:30 (or 12,5 hours where 0,5 hour=30min) and R2 = 15,5 UAH/hour and since you want to calculate salary, you should multiple 12,5*15,5=193,75.
Follow next steps to achieve the desired result:
Write next formula in S2: =Q2*24*R2 (since time stored in Excel as part of a day, we need to multimply by 24 . Your value 12:30 is actually 0,520833333333333 ,you can see it if you'd format Q2 as number. Myltiplication 0,520833333333333 by 24 gives you exactly 12,5)
Format your S2 as number
the result would be 0,52*24*15,5 = 193,75 and this value is what you actually need.

Resources