Negative time span values in Excel - 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.

Related

How can I (easily) format long intervals (over 30 days, but including time components)

I am using a spreadsheet to record the intervals between certain medical events. Each event's timestamp is recorded in one column, so the interval between each event is the difference between consecutive cells (and the time since the last event uses now()).
First problem: I want to display the interval in days, hours and minutes. None of the built-in formats will do this, they report the days remaining after discarding complete months. So I am using this expression:
TEXT(TRUNC(C2-C1),"0") & " days " & TEXT(MOD(C2-C1,1),"hh "" hrs "" mm ""mins""")
which (e.g.) shows "46 days 13 hrs 44 mins". I was hoping there was a way to format a date/time value to show this rather than making the cell a string value, but I haven't been able to find one.
Second problem: I want to display the average value of all completed intervals in the same format. Because I can't average the string values produced by the previous expression I need to average the numeric equivalent (which I'd prefer not to have visible in the sheet) and then convert it to a string as for a single interval.
I can probably do this with a similar approach (if I don't run out of characters to enter the formula) but it seems to me that there must be a better way.
Ideally there is a solution which will work in Excel 2010. Has anyone solved a similar problem before and can give me some pointers?
Thanks, T
Edit: Some data to show what I am working with (I hope the image is readable). Here's a few lines from the sheet.
The formula for H2 etc. is
=IF(G2="c",NOW()-C2,"")
I5 is calculated as the difference between C5 and Cprev (where prev is chosen so that D5 and Dprev are both set). Obvious extension of this for J and K.
M2, M3 and M4 are respectively
=AVERAGEIF(K5:(INDIRECT("K"&(ROWS(K:K)))),"<>0")
=AVERAGEIF(J5:(INDIRECT("J"&(ROWS(J:J)))),"<>0")
=AVERAGEIF(I5:(INDIRECT("I"&(ROWS(I:I)))),"<>0")
Now, I can use a custom format for the values in H, I and J, and for M3 and M4, because these values will never exceed a few days. But values in K and M2 will be somewhere around 100 so I can't just format the raw value.
With custom formatting applied:
Here K6 and M2 say "27 days.." not "87 days..". That's what I'd like to fix nicely, hopefully without populating additional cells or writing a 3gl function to do it.
Date_Times in Excel are stored as days (with the decimal part representing parts of a day).
If the date matters, rather than just a number of days, then day zero is 1899/12/31.
Your first thought was right - do with formatting, not by turning a number into a string.
Entering date into A and time into B, with C=A+B is a good start, so that you can E.g. subtract one point in time from another without having to do anything about straddling midnight, month-ends etc, calculate averages etc.
Consolidating the comments already here: per https://support.microsoft.com/en-us/office/format-numbers-as-dates-or-times-418bd3fe-0577-47c8-8caa-b4d30c528309 you cannot get d for days above 31 (and it won't accept a format 0 hh:mm:ss )
I suggest that you do all your calculations using just numbers to get to say column M, and in N2 put =M2 etc, so you have the same values twice.
Then for formatting, use Format Cells | Number | Custom.
In column M put 0 "days".
In column N put hh "hours" mm "minutes".

Excel: Comparing Times to get minutes apart

I'm trying to get the + and - between two times but if the time I am subtracting from is greater than the time I am subtracting it gives me the difference in 24 hours.
I would like it to show this:
Perhaps this needs to be done in VBA. Have column B be the time to compare to and put max time apart as 12 hours, either positive or negative. So if the time is different by over 12 hours it becomes a negative instead of positive or vise-versa?
#tysonwright that is the best way to do it. Thanks. I assumed there was a better process but apparently there is not. So with that, I change the date to the next day to get 23:59 to 0:01.
If your clockin is in A1 and your Schedule is in A2, your Difference should be:
=IF(A1>B1,TEXT((A1-B1),"-h:mm"),TEXT((B1-A1),"h:mm" ))

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

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)

Resources