How to Calculate difference between hours when the cells contain date in date format? And there are some conditions too - excel

Need difference between Created & Closed In Hours along with below
The TAT hour to be calculated between 9:00 am to 09:00 pm which is working hours, if created time us 09 pm then it should be considered from next day 9 am.
Sunday is Holiday
I have tried:- =Created-Closed Date, but don't know how to exclude Sunday and calculate only shift hours.

Yes! Finally Self Got Answer For it.
Here is that
=(NETWORKDAYS(A2,B2)-1)*("21:00:00"-"09:00:00")
+IF(NETWORKDAYS(B2,B2),MEDIAN(MOD(B2,1),"21:00:00","09:00:00"),"21:00:00")
-MEDIAN(NETWORKDAYS(A2,A2)*MOD(A2,1),"21:00:00","09:00:00")

Related

Time elapsed between two dates (in "working hours")

I must calculate in Excel the time that a Tech Support takes to resolve a problem, in his working hours.
Example :
Ticket created at : 04:00 PM the Friday 3 June 2022
Ticket resolved at : 10:00 AM the Monday 6 June 2022
Working hours : 09:00 AM to 12:00, and 01:30 PM to 06:00 PM
So the result will be : He took 3 hours to resolve the problem.
I'm open to any formula, even in VBA :)
Thanks.
EDIT 1 :
I'm using this formula currently :
=IF((NETWORKDAYS(D2;E2;PublicHolidays)-1)*(Working_Hours!$B$3-Working_Hours!$B$2)
+IF(NETWORKDAYS(D2;E2;PublicHolidays);MEDIAN(MOD(E2;1);Working_Hours!$B$3;Working_Hours!$B$2);Working_Hours!$B$3)
-MEDIAN(NETWORKDAYS(D2;E2;PublicHolidays)*MOD(D2;1);Working_Hours!$B$3;Working_Hours!$B$2)
+(NETWORKDAYS(D2;E2;PublicHolidays)-1)*(Working_Hours!$E$3-Working_Hours!$E$2)
+IF(NETWORKDAYS(D2;E2;PublicHolidays);MEDIAN(MOD(E2;1);Working_Hours!$E$3;Working_Hours!$E$2);Working_Hours!$E$3)
-MEDIAN(NETWORKDAYS(D2;E2;PublicHolidays)*MOD(D2;1);Working_Hours!$E$3;Working_Hours!$E$2)<0;0;(NETWORKDAYS(D2;E2;PublicHolidays)-1)*(Working_Hours!$B$3-Working_Hours!$B$2)
+IF(NETWORKDAYS(D2;E2;PublicHolidays);MEDIAN(MOD(E2;1);Working_Hours!$B$3;Working_Hours!$B$2);Working_Hours!$B$3)
-MEDIAN(NETWORKDAYS(D2;E2;PublicHolidays)*MOD(D2;1);Working_Hours!$B$3;Working_Hours!$B$2)
+(NETWORKDAYS(D2;E2;PublicHolidays)-1)*(Working_Hours!$E$3-Working_Hours!$E$2)
+IF(NETWORKDAYS(D2;E2;PublicHolidays);MEDIAN(MOD(E2;1);Working_Hours!$E$3;Working_Hours!$E$2);Working_Hours!$E$3)
-MEDIAN(NETWORKDAYS(D2;E2;PublicHolidays)*MOD(D2;1);Working_Hours!$E$3;Working_Hours!$E$2))
Well, this is a start, but you need to think about how to deal with multiple days etc:
All the example times shown are formatted as dd.mm.yy hh:mm, even consider putting the Day start & end as just the hour.

how to calculate time difference in excel working hours only

How can I calculate hours worked on a project using specific working hours that aren't the same each day?
So Monday - Friday I work 7 am-7 pm, Saturday 9 am -1 pm and I take Sunday off (lucky me). If i start a project on the 1st March 10 am and finish on the 5th March at 9 am how can I calculate an answer of 27 hours ??
I have two cells date/time start and date/time finish. I have multiple rows to do this to and several time points but this essentially will work the same.
I hope makes sense.
Edit - Solutions tried and opposing results
You will need a helper column with this formula:
=24*(SUMPRODUCT((TEXT(ROW(INDEX(AAA:AAA,$F$1):INDEX(AAA:AAA,$F$2)),"dddd")=A1)*(C1-B1))-IF(TEXT($F$1,"dddd")=A1,MOD($F$1,1)-B1,0)-IF(TEXT($F$2,"dddd")=A1,C1-MOD($F$2,1),0))
Then sum that column.
Here it is in one formula using NETWORKDAYS.INTL
=IF(DATEDIF(F1,F2,"d")>1,NETWORKDAYS.INTL(F1+1,F2-1,"0000011")*12+NETWORKDAYS.INTL(F1+1,F2-1,"1111101")*4,0)+IF(DATEDIF(F1,F2,"d")>0,(MOD(F2,1)-IF(WEEKDAY(F2,2)<6,TIME(7,0,0),TIME(9,0,0)))*24+(IF(WEEKDAY(F1,2)<6,TIME(19,0,0),TIME(13,0,0))-MOD(F1,1))*24,(F2-F1)*24)

Day and night time calculation in Excel sheet

In MS Excel sheet I want to calculate the time I work in the day hours i.e. the hours between sunrise and sunset, and any hours after sunset.
An example might be that I start work at 06:00 am and finish at 09:00 pm.
Sunrise is 07:00 am and sunset is 06:30 pm.
The total time worked all day would be 15 hours.
Time worked during the day (between sunrise and sunset) would be 11 hours and 30 minutes.
Time worked at night would be 3 hours and 30 minutes.
Here is my simple solution :)
Just copy and paste the formulas in E5 and F5 and drag them down to calculate the time as per your requirement
Formula in E5 is:
=IF(AND(B5<$C$1;C5>=$G$1);$G$1-$C$1;IF(AND(B5>=$C$1;C5<$G$1);C5-B5;IF(AND(B5<$C$1;C5<$G$1);C5-$C$1;$G$1-B5)))
Formula in F5 is:
=C5-B5-E5
It is important to note that my solution covers all four possibilities which are:
1. Start work at night and end at night
2. Start work at day and end at day
3. Start work at night and end at day
4. Start work at day and end at night
:)
Below is what I think you want to achieve.
Since I have included sunrise and sunset as columns it means that you can add another column Date and then you can track each new day as a new row; since sunrise and sunset change each day you cannot simply have one cell to set the times for these for all your data.
The formulas:
A2
06:00:00
B2
21:00:00
C2
07:00:00
D2
18:30:00
E2
=IF(B2>D2,D2,B2)-IF(A2<C2,C2,A2)
F2
=(B2-A2)-E2
Notice that I have written the times in 24 hour clock.
This is important since Excel cannot tell the difference between 07:00 if it is AM or PM and you cannot pass AM or PM in the cells.
The formula in E2 will check if you have started before sunrise or not. If you have started before sunrise then it will just use the sunrise time.
Then it will do the same check for sunset; using sunset if you have worked after sunset.
After that it will just subtract the two numbers and return the time worked.
F2 will just simply subtract the start and end time to work out total time worked all day and will then just subtract the time worked in the day.

Specific overtime calculation

I'm working on a simple timesheet that should calculate overtime1 and overtime2, I just cant figure out a good way to fix it.
Normal workday on 8hrs from 07:00 to 17:00 give no overtime (eg 07:00 to 16:00 or 08:00 to 17:00), but when time exceeds 8 hours I would like to get the exceeding hours in a cell.
My business rules are:
1) Any work greater than 8 hours between the hours of 06:00 to 20:00 get paid as overtime1.
2) Any work less than 8 hours will not generate any overtime2 even if after 20:00
3) Any work performed earlier than 06:00 or later than 20:00 get paid at the overtime2 rate.
Example 1: Working from 07:00 to 18:00 would get a value of 3 hours of overtime1
Example 2: Working from 14:00 to 22:00 would generate 0 hours of overtime2.
Example 3: Working 05:00 to 21:00 would give overtime1 6 hours and overtime2 2 hours (1 hour before 06:00, 1 hour after 20:00).
This is a pretty easy problem to solve if your data is laid out smartly. With column A as clock in time and column B as clock out time, use this formula as a helper to determine if you should use the clock in time or your base of 06:00:
=IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2))
Then use this formula to determine if you should use clock out time or 20:00:
=IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))
Then you subtract the two to get fractions of a day, multiply by 24 to convert to hours, then subtract 8 to get hours of overtime1. Combined in a super formula it looks like this in C1:
OT1: =IF(OR(ISBLANK(A1),ISBLANK(B1)),"",(IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))-IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2)))*24-8)
Remember, Excel formats dates where 1 = 24 hours. Also, I added in an OR(ISBLANK(A1),ISBLANK(B1)) statement to make sure you get a null string if one of the values is blank.
Starting on the overtime2, you need to split it into two parts: before 06:00 and after 20:00. The first part checks if the clock in time is earlier than 06:00 and if so figures out how many hours. The formula ultimately ends up being:
=IF(IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2))<=6/24,(6/24-(A1-FLOOR(A1,2)))*24,0)
For after 20:00, the same pattern is used. Figure out how many parts of a day were logged after 20:00. The final formula ends up being:
=IF(IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))>=20/24,((B1-FLOOR(B1,2))-20/24)*24,0)
Finally, to figure out the total number of overtime 2, just add the two formulas together in D1:
OT2: =IF(OR(ISBLANK(A1),ISBLANK(B1)),"",IF(IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2))<=6/24,(6/24-(A1-FLOOR(A1,2)))*24,0)+IF(IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))>=20/24,((B1-FLOOR(B1,2))-20/24)*24,0))
It's just about getting the logic right and understanding that Excel treats 07:00 as a decimal equal to 7/24, for example.
OT1
=IF(NOT(AND(Sheet1!$A2>=7/24,Sheet1!$B2<=17/24)),MIN(20/24,Sheet1!$B2)-Sheet1!$A2-9/24,0)
OT2
=IF(MIN(20/24,Sheet1!$B2)-Sheet1!$A2-9/24,MAX(B2-20/24,0),0)

How do I calculate a date by adding 30 calendar days, where end date must be business day?

I'm wondering if this is possible. I am creating a spreadsheet to track project due dates. Each project must be completed by the 30th calendar day, but must be turned in on a business day.
Currently, I am just adding 30 days to the start date but this means some due dates aren't always accurate. For example, if the 30th day is Saturday, April 2nd, then the real due date would be Friday April 1st.
Is there a way to construct a conditional such that the due date equals the 30th calendar day, unless that falls on a weekend / holiday, where it then falls on the next earliest business day?
I've been struggling to figure out a way to do this.
For English settings in Excel, with a date in A1, in B1 enter:
=IF(TEXT(A1+30,"DDDD")="Sunday",A1+28,IF(TEXT(A1+30,"DDDD")="Saturday",A1+29,A1+30))
This simple-minded approach only handles Saturdays and Sundays, not arbitrary holidays.
I would prefer more elegant way like using WORKDAY.INTL
=WORKDAY.INTL(A2+31,-1,1,E2:E)
Explanation: start date + 31 days (1 day more than maximum calendar days)
then subtract 1 working day - going to last previous working day
Reason: because this formula does know when are weekends (by using variables) and also knows to skip hollydays by a custom list.
here is an example sheet you can use
Revised after comment:
Try this:
=(A4+30)+CHOOSE(WEEKDAY(A4+30),1,0,0,0,0,0,-1)
Your date, in A4, + 30 days, then add an amount of days until the next workday. If A4 + 30 is a Saturday it will subtract 1 day, a Sunday will add 1.

Resources