Subtracting seconds in Excel - 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

Related

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.

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

formula works fine with the cell in hh:mm:ss format, but the same formula gives incorrect value in cell with mm:ss format

my data appears as this
WAIT
29:45:00
2:41:14
46:06:00 ' <---
0:25
19:23 ' <---
2:25:12
37:36:00
2:12:24
1:34:35
1:54:13
13:00:53
when i am applying the formula =HOUR(F7)*3600+MINUTE(F7)*60+SECOND(F7) to 46:06:00, it gives the correct answer, however when same formula is applied to the 19:23, excel recognizes 19:23 in HHMMSS format, however it is MMSS...
i have a huge data base and it would be very difficult for me to change it manually....
please help...
By default, Excel treats numbers in the format xx:xx or x:xx as h:mm, and NEVER as mm:ss.
I don't see a way to transform the values into valid hh:mm:ss, since there is no handle for Excel to determine which number is lacking the hour part.
The best option is to change the routine that writes the data and have the hour entered as 00: before a mm:ss value.
As long as the data column is formatted as text, you could test how many colons exist in the string and apply a different calculation depending on whether there is 1 or 2. If there is only 1 colon, then use what Excel thinks is the hours as the minutes, and what Excel thinks are the minutes as the seconds.
=IF(LEN(F7)-LEN(SUBSTITUTE(F7,":",""))=2,HOUR(F7)*3600+MINUTE(F7)*60+SECOND(F7),HOUR(F7)*60+MINUTE(F7))
It's difficult to tell whether your data is text-formatted or not - HOUR/MINUTE/SECOND functions work just as well on text-formatted times as real times. What result do you get with this formula?
=ISNUMBER(F7)
If that gives you FALSE then your "times" are text formatted in which case this formula will give you the total seconds:
=LOOKUP(999,({"","0:"}&F7)+0)*86400
format result cell as general
That will interpret 19:23 as 19 minutes 23 seconds as required. For 46:06:00 you will get the result 165960, the total seconds in 46 hours and 6 minutes. Your formula is giving you just the seconds from the remainder after multiples of 24 hours are removed, as pnuts points out. That's because
=HOUR("46:06:00")=22
If you want the result to be 79560 then you can add a MOD function like this:
=MOD(LOOKUP(999,({"","0:"}&F7)+0),1)*86400
The LOOKUP function works here because it exploits the fact that concatenating "0:" to the start of 19:23 (and then doing a calculation like +0) converts 19:23 (19 hours 23 minutes) to 0:19:23 (zero hours, 19 minutes, 23 seconds) but concatenating 0: to 46:06:00 and then adding 0 gives you an error.
Guyz, thx for answering the question..
here is what i tried-
first i applied
=TEXT(D2,"hh:mm:ss") formula to the cell value and then used =IFERROR(SUMPRODUCT(("00:"&d2)+0),d2), so 19:23 got changed to 00:19:23..
It worked though i had to do some manual work on the data...

formatting only in minutes in excel cell

I am using excel 2007.I need help in formating cells to display only minutes.
Suppose if the actual duration is 30 seconds,it should show .5
The simplest way to do this is to multiply your time value by 1440 (the number of minutes in a day), so if A1 contains 5:20 then use this formula in B1 to get 320
=A1*1440
This solution will work for any time value, even values over 24 hours, e.g. converts 33:20 to 2000
Edit: I note you mention "formatting" - you could custom format A1 as [m] (you need to include the square brackets) which would also display 320 or 2000 in my examples without need for a formula in another cell.......but that will only display an integer so you can't use that to get 0.5 for 30 seconds
=(HOUR(A1))*60+MINUTE(A1)+ IF(SECOND(A1)>30, INT(1+ SECOND(A1)/60), SECOND(A1)/60)

Excel 2007 - Time exceeding 24 hours where [h]:mm doesn't work

I'm building a spreadsheet to calculate aircraft flying hours which will exceed 24. I am aware of the [h]:mm format to force Excel to not use units of days.
I want to set a threshold number of hours which I will subtract a running total from. However with the [h]:mm format set if I enter 1 in the cell, for 1:00, instead it interprets it as 24:00 - one day.
How can I avoid this, so that I can do simple arithmetic with hours and minutes exceeding a 24 hour period?
Type 1:0 or even just 1: instead of just 1
If you type 1 excel interprets this as the value 1, which is date serial value to 1 day (actually 1/1/1900)
If you type something that looks like a time (eg contains a :) then excel interprets it as a string representation of a time and converts it to the corresponding serial value (0.041666667 or 1/24) and applies the format so it displays as 1:00

Resources