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

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) #######.

Related

Summing time in Excel table gives very large number?

Can anyone help please?
When I sum two time value such as 09:00 and 22:30 I can get a result of 31:30 by formatting the cell using [h]:mm:ss.
However, whenever I try and do this with a subtotal row on an Excel table, it’s gives ridiculously large numbers?! For example, if I do the same as my first example, I get a result of something like 2million?!
Can anyone explain what I’m doing wrong? It works fine outside of the table but I need to use a table since I am populating the values from a PowerAutomate flow.
As above please see my example
Excel uses numbers to represent dates and times. It starts with 0.0 as midnight on Jan 1 1900 , 1.0 is midnight on Jan 2 1900 etc, each whole number is one day since Jan 1 1900 and times are represented using a fractional value. E.g. 0.5 is noon on Jan 1 1900, 2.25 is 6:00:00 am on Jan 3 1900 and so on.
When you sum date-times in Excel , it just sums these numerical values.
In your test case of adding two date-times 9:00am and 22:30 (=10:30pm) and getting 31:30, you are probably summing two fractional values only (e.g. 0.375 + 0.9375 ) to get a whole day and a bit ( = 1.3125) which the format you are using shows you 31:30.
Typically, date-times in Excel are both the whole number and the fraction. For recent dates these numbers can be quite large 44,926.5 is noon on Dec 31st 2022 for example. If you sum even a few of these, as numbers, and try to represent them as dates you will get an absurdly large number.
In order to help you better you will need to tell us the actually numerical value (not the formatted representation) of the underlying cells.

Subtracting seconds in Excel

I'm trying to subtract start time from end time to get duration, ie:
1:02 - 0:10 = 0:52
But what I'm getting is:
0:92
I'm using the 0\:00 format. Other suggested formats, such as [mm]:ss, are turning my data into numbers that I don't undestand, ie 1:02 becomes 146880:00
I just want to quickly enter a bunch of times, subtract one col from the other and be done with it.
Does anyone know a way to do that?
Solutions follow an explanation of the results showing in the question.
The format 0\:00 is really the format 000 with a colon character inserted between the first and second digits.
If a cell holds the value 102 and has a format of 0:\00 it will show as 1:02 in the worksheet but behind the scenes its value is still 102. So
1:02 - 0:10 = 102 - 10 = 92 = 0:92 in 0\:00 format
To understand the result with the [mm]:ss format, you need to understand how dates (and time) values are represented in Excel. There is a reasonable explanation on this webpage from Chip Pearson
First, as a date/time value 102 is equivalent to 0:00 on 11 April, 1900 as it is 102 days from Excel's day/time zero. Second, the format [mm]:ss expresses this elapsed time in minutes and seconds. So
102 days = 102*24*60 minutes = 146880 minutes
which gets displayed as 146880:00 in [mm]:ss format
There are a couple of ways you might resolve your problem.
The first involves entering data differently. A time can be entered directly into the worksheet as hours:minutes:seconds. So 1 minute and 2 seconds can be entered as 0:1:2 (or 00:01:02 or any variant such as 0:01:2 or 00:1:02). This is probably less convenient than just entering 102. By default, numbers entered in this way will display in a hh:mm:ss format but you can suppress the display of hours by changing the format to mm:ss or [mm]:ss. The latter should be used if any of your time values are 60 minutes or more since, the former will suppress the display of hours - for example, entering 0:61:2 (61 minutes and 2 seconds) displays as 01:02 with the former but as 61:02 with the latter.
Note that if you just enter 1:2 rather than 0:1:2 Excel interprets this as 1 hour, 2 minutes and 0 seconds and will display as 02:00 using format mm:ss or as 62:00 using [mm]:ss.
The second way allows you to enter the data as before using the 0\:00 format but requires the use of formulae to convert your entered value into seconds - so, for example, an entered value of 102 is intended to represent 1 minute and 2 seconds, gets correctly displayed as 1:02 but is converted behind the scenes to 62 seconds.
If A1 and B1 contain the entered values the then formula for A1 less B1 is
=(INT(A1/100)*60+A1 - 100*INT(A1/100))-(INT(B1/100)*60+B1 - 100*INT(B1/100))
This formula calculates its result as a number of seconds.
If this result is placed in cell C1 then the formula
=100*INT(C1/60)+(C1-60*INT(C1/60))
converts C1 to a result suitable for displaying with the 0:\00 format
Alternatively, the result in seconds can be converted to days by dividing by 24*60*60 = 86400 and displayed using a time format such as [mm]:ss

Difference between date and time w/out work week Excel

I am trying to determine the amount of time a procedure takes. I have the Start Date and Start Time as well as End Date and End Time. What I need to do is display the result in hours:minutes:seconds BUT I need to use a 7 day work week rather than a 5 day work week. Here is a sample data point:
Cell A1 and C1 are both formatted as short dates. Cell B1 and D1 are formatted as times. The formula in E1 is =((C1+D1)-(A1+B1)) and E1 is formatted as a custom number [h]:mm:ss . The problem I am having is calculating through a weekend. The times just don't add up and I think it is because of the type of work week that Excel uses.
Any help is appreciated, thanks!
First, you need to know / understand that any number is a potential date / time for Excel and that Excel knows only datetime (and not date and time separately).
Here is why: anything before the decimals-placeholder are the days since December 31, 1899. So, the number 42,000.00 is also a date. It is the 42.000th day after December 31, 1899 which is December 27, 2014.
Anything after the decimal-placeholder is considered a fraction of a day. So, the value 0.5 is half a day and therefore the time 12 noon while 0.75 is 6 in the afternoon (pm).
Hence, all of the dates you are showing in your above examples may have also times associated with them and all the times you are showing might also have a date associated to them. Just change the number format on all of these cells to general numbers and you should see only Integer values for the dates (without any decimal places) while all the times should contain only be a fraction of 1 and should all have a 0 before the decimal-placeholder.
If that is not the case, then you have just found your culprit.
With the above knowledge it is also easy to calculate the difference between two dates or times. The difference is a simple addition or subtraction: 42,000.00 + 100 + 0.5 = 100 days after December 27, 2014 at 12 noon = April 6th, 2015 # noon. So, you can just add the numbers and subtract them as you have done. If you wish to subtract two days to account for a weekend then you can simple subtract 2.
Now, let's have a look at your above example:
June 23, 2016 converts to 42,544 while 07:41 in the morning converts to 0.32. So, we have here 42,544.32 while the ending datetime converts to 42,545.47.
The difference is calculated correctly as 1.15. That means that there is a difference of one day and the fraction of a day of 0.15 * 24 (hours in a day) = 3 hours and 36 minutes.
I guess the real problem you are having is to show the result correctly. If you show it as a date then it is one day after December 31, 1899 = January 1st, 1900. If you show the result as a time then you only get to see the 03:36:00. The best is probably to show the difference as a number only as 1.15. If you want to see the result differently then you will have to adjust the resulting view to your needs. For example:
= 1.15 * 24 = 27.6 hours difference between the start date/time and the end date/time.
Let me know if you have any further questions.

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?

Negative time span values in Excel

I develop an app that is able to export its data into an Excel file. Some of the cells do contain negative time span values. I know that Excel is developed in a way (based on the 1900 date system) that leads to issues there so I enabled the 1904 date system. Otherwise I'm not able to show negative time values.
What I have is a time value in B12 (format of all cells is [h]:mm:ss).
Now in E12 I want to show the result of subtracting 40 hours of C12. For some reason, instead of -8 it shows +16. So I double checked in E13 to see whether the calculation itself is different. But there it works as expected.
One possible workaround which I can think of can be seen in E16. There I have a reference cell that contains 40 hours time as value. But I don't like it as this 40 h is some kind of uninteresting content here and should not pop up anywhere.
Another possible workaround would be to use TEXT as the cell format but then my apps users will loose the possibility to do further calculations with the exported values.
So the question is: Why is E12 showing +16 hours? What am I missing here?
What am I missing here?
You are making incorrect assumption on how first parameter of TIME() function works. 40 does not mean 40 hours in result, but (I quote):
Hour Required. A number from 0 (zero) to 32767 representing the hour. Any value greater than 23 will be divided by 24 and the remainder will be treated as the hour value. For example, TIME(27,0,0) = TIME(3,0,0) = .125 or 3:00 AM.
Do you really need that function? You can calculate 40 hours like 40/24, i.e. with =C12-40/24 in E12. Then you get expected time -8:00.00.
If you have value >= 24:00:00 at some other location (e.g. A1), you can convert it using =DATEVALUE(A1) + TIMEVALUE(A1). Such values have their date part. But when you know how Excel date/time system works (1 = 24 hours, so 1/6 = 4 hours, 1.5 = 36 hours, etc...), you do not need to bother with these functions and you can calculate many time values directly as I shown you.

Resources