EXCEL - Substract two 24hr time format cells with seconds and miliseconds - excel

Okay, so I have two cells:
Start End
11:31:37.644 11:31:51.269
I'd like to subtract the two and return the remaining time which should equal something around 14 seconds.
Edit for more information:
My values I'm inputting are like so:
113137.644
113151.269
and I have a custom formatter set to: 00\:00\:00.000 to display what you see at the very top.

It would be better if you could input the values as real time values then you can just use a simple subtraction
=A2-A1
...but with the values as they are you can do a conversion and subtraction all in one using TEXT function, e.g. in A3 use this formula
=TEXT(A2,"00\:00\:00.000")-TEXT(A1,"00\:00\:00.000")
format A3 as [h]:mm:ss.000 to get 0:00:13.625 for your example
Assumes times are on the same "day", if you need to pass midnight you can revise formula to
=MOD(TEXT(A2,"00\:00\:00.000")-TEXT(A1,"00\:00\:00.000"),1)

You need to use the MID function to grab each section of the time, like this:
=MID(A1,1,2)&":"&MID(A1,3,2)&":"&MID(A1,5,2)&"."&MID(A1,8,3)
This will change 113137.644 to 11:31:37.644.
You can then do the math on it like this (all in one cell, but broken up here for readability):
=TEXT(MID(B1,1,2)&":"&MID(B1,3,2)&":"&MID(B1,5,2)&"."&MID(B1,8,3), "hh:mm:ss.000")
-
TEXT(MID(A1,1,2)&":"&MID(A1,3,2)&":"&MID(A1,5,2)&"."&MID(A1,8,3),"hh:mm:ss.000")
That should give you 0.000157697. Change the field's custom format to hh:mm:ss.000 to give you 00:00:13.625.

You can use this formula to convert your values to time
=(LEFT(A1,2)+(MID(A1,3,2)+RIGHT(A1,LEN(A1)-4)/60)/60)/24
You can then subtract and convert back using the following formula
=TEXT(B1,"hhmm")&TEXT(MOD(B1*24*60,1)*60,"00.000")
But it would probably be better for you to actually use proper decimal values in fractions of days or hours rather than this you can't calculate anything with.

Related

Converting date from general type to date, 9/29/1986 0:00:00 to dd/MM/YYYY CSV file

i have hit wall after trying INT, TIMEVALUE and all other date formatting on the dates in CSV file.
I was able to change few dates using INT and then change the date format but few dates (highlighted in yellow) i am not able to convert to date format. Originally it is string, which i tried changing to Number and Date type before applying formulas but still its not getting formatted correctly.
i have tried MID/LEFT etc. to extract part of it but when joining these parts using "" & "" converts to text and converting it to date resulted in long ##### output, did tried excel advance option ticking Use 1904 date system.
Any help in right direction is much appreciated. i have not found any duplicate question similar to my format, closest i found didnt have time stamp so that formula didnt work either.
Take a look at your date format. The first digit is either 1 or 2 characters and you need to take that variation into account. the nice thing is based on your data that the days is always 2 digits. this simplifies things a little.
Lets start with the basics and assume your first string of a date is in A2. Let us start simply by striping out the numbers from the text one segment at at time while being generic about the position and number of character. So in order to pull out the number for the month, use the following formula:
=LEFT(A2,FIND("/",A2)-1)
Find will look for the position of the / character in the string and return its number. in this case it should be 2. This means its a single digit month. So we only need to pull 1 digit. In the general sense 1 less than the position of the /.
The next task will be to pull the digits for the day. We can do that using a similar formula. This time lets use MID instead of left. In order to for MID to work, we need to define the starting point. This time the general case of the start point will be the first character after the first /. The other nice part about this is we know the number of characters to pull will always be 2. As such you can use the following formula to pull the month:
=MID(A2,FIND("/",A2)+1,2)
(note if your day digits were not consistently 2 then you would have to measure the number of characters between the two / characters and replace the 2 in the formula with you calculation)
In order to pull the year the process is basically the same as for the month with some minor tweaks. The resulting formula I am suggesting is:
=MID(A2,FIND("/",A2,4)+1,4)
Now the reason I used 4 as the starting position for the find is to make the formula work for the case where days could be a single digit. It the closest the second / can be to the start.
now that you have all that you need to combine it together to make the date. This is where the DATE formula comes into play. It works in the following format: DATE(Year, Month, Day). So now we simply grab each of the individual formula and build the DATE formula which should wind up looking like the following:
=DATE(MID(A2,FIND("/",A2,4)+1,4),LEFT(A2,FIND("/",A2)-1),MID(A2,FIND("/",A2)+1,2))
if you get a date that is just bunch of number format the cell to display the date in the format of your choosing.

MS-EXCEL: time averages + numbers to time

I am working on a project for my work and I am having an issue trying to take times and averaging them out. I am getting a #DIV/0! When I convert the cells to XX:XX.
Below are screen shots of the same data and function. The top is with regular numbers the bottom is where I formatted all cells to display in time XX:XX.
Numbers (works fine)
= AVERAGE(H29,J29,P29,V29,AE29,AJ29)
Time (XX:XX) however I get the #DIV/0!. The AVERAGE Function I am using looks like this:
=AVERAGE(H29,J29,P29,V29,AE29,AJ29)
Also, if possible, could I enter the data in regular numbers (not time) then have it display in average time (hours:min) on the right?.
Thank you.
When working in Excel with dates and times always leave the value as a number and apply formatting. where you have tried to enter a time in hh:mm, you need to show the hours even if they are blank, or the value of that cell becomes a string. But you can't average a string.
These are valid entries 00:23 or 0:23
:23 is not valid time, so is treated as a string
Valid times are stored as a number where 24 hours = 1.
Times are stored as a fraction for example 0:45 = 0.031250000
To fix it, add in the missing leading zeros, or if you have explicitly entered text strings: type a 1 in any cell and copy it, then select all of your time cells and do paste special multiply. That will convert them back to numbers and your average should work. Then apply your custom formatting of required .
I would suggest that users enter the time in minutes eg 17, 23, 92 and you average these, but for the conversion I suggest that you use a formula like this:
=AVERAGE(H29,J29,P29,V29,AE29,AJ29)/(24*60)
This will convert the average time in minutes, into a fraction of a day.
You can then put custom formatting onto the cell to change the format to hh:mm

Convert date e.g. Jun 05 2016 08:00:00 to dd/mm/yyyy hh:mm:ss

Got input such as in the topic title.
Trying to figure out how to convert this into UK date and time to be used in calculations.
I've looked at some methods on Google such as using text to columns, but I don't think this is what I'm looking for...
Thanks!
Edit: month is always in abbreviated format.
Edit 2: I should mention that I'm in the UK, and it doesn't seem to convert US date automagically.
Edit 3: Data:
Jun 05 2016 08:00:00 to dd/mm/yyyy hh:mm:ss
Assuming that your source date is a string and it is in the cell D10, the first thing you need to do is convert it to an excel date time serial. In the Excel Date Time serial there are a few things to note.
The integer portion of the number represents the number of days since Jan 1 1900 in windows and I think 1904 on mac
The decimal portion of the number represents the time in fraction of a day. 0.5 would represent noon. Valid Excel times for VBA are 00:00 to 23:59:59. 24:00 is not a valid time, though it will work with some excel formulas
So in order to convert your string to an Excel date serial we will need to rip out the components and dump them into the DATE() function. The date function consists of three arguments:
DATE(year,month,day)
Pretty straight forward with the exception that those values need to be numbers. Why dont we start pulling your information going from the largest unit to the smallest unit.
Thankfully your string is of consistent length. You have leading 0 for your single digits so they will occupy the same space as double digits. So this method will work until the year 9999, but I don't think we are too worried about that right now.
In order to pull the year we look at where it is in your string and how long it is. So by simply counting we know it starts in the 8th character position and its 4 characters long. We use this information with the MID() function
=MID(D10,8,4)
In order to pull the month it get a little more complicated since we need to convert it from an abbreviation to a number. There are several ways of doing this. You could go for a long IF statement which would wind up repeating the pull of the month a 11 times. Instead I decided to use the MATCH() function and built an array of month abbreviations inside it. The MATCH() function will return the number/position of what you are searching for in the provided search list. So as long as we enter the months in chronological order, their position will correspond to their numeric values. As such our formula will look like:
MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0)
The LEFT() function was used to pull the month abbreviation from your string. The 0 at the end tell match to look for an exact match. Important to note, this match method is not case sensitive.
Now to get the day we employ the same principals that we did for pulling the year and we wind up with:
=MID(D10,5,2)
We can now substitute each of the formulas for Year Month and Day into the DATE() function and we will get the date portion of the excel date serial. The formula should look like the following:
=DATE(MID(D10,8,4),MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),MID(D10,5,2))
Now you need to tack on your time portion or figure out the decimal portion. In order to do this I would first recommend trying the TIMEVALUE() function. Since time formats tend to be a lot more standard in format than dates, there is a much higher probability that it will work for you. In order to use TIMEVALUE(), the time portion needs to be ripped from the string. This can easily be done with the RIGHT() function as follows:
=RIGHT(D10,8)
That will give you just the time portion which can then be substituted into the TIMEVALUE() function and looks like:
=TIMEVALUE(RIGHT(D10,8))
If the TIMEVALUE() function does not work for you, then you will need to strip out the hour minutes and seconds and dump their results in to the TIME() function. Do this in the same way you pulled the year and the day for the DATE() function. Just update your character counts. TIME() uses three arguments as follows:
TIME(HOUR,MINUTES,SECONDS)
Now that you have figured out your date portion and your time portion all you need to do is add them together to get all the information into one cell. The resulting formula will look like:
=DATE(MID(D10,8,4),MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),MID(D10,5,2))+TIMEVALUE(RIGHT(D10,8))
Where ever you windup placing that formula, remember to change the formatting on the cell to a custom date. Enter the cell custom format as in the image below.
If you have a list of date times to convert in a column, simply copy your formula and formatted cell down as far as you need to go.
Proof of Concept
Formulas used
For more information on the functions used in the formulas above, follow the links below:
MATCH
DATE
TIMEVALUE
RIGHT
MID
LEFT
{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"}
The { } are used to build a custom list or static array.
If you don't want to write a macro in VBA, a cell formula would work if the format is as you specified:
I am assuming the data is in cell B3
=MID(B3,5,2) & "/" & IF(LEFT(B3,3)="Jan","01",IF(LEFT(B3,3)="Feb","02",IF(LEFT(B3,3)="Mar","03",IF(LEFT(B3,3)="Apr","04",IF(LEFT(B3,3)="May","05",IF(LEFT(B3,3)="Jun","06",IF(LEFT(B3,3)="Jul","07",IF(LEFT(B3,3)="Aug","08",IF(LEFT(B3,3)="Sep","09",IF(LEFT(B3,3)="Oct","10",IF(LEFT(B3,3)="Nov","11","12"))))))))))) & "/" & RIGHT(B3,13)
The DATEVALUE function expects a comma between the day and year; the REPLACE function can add that in. The TIMEVALUE function should be able to read that time without modification.
=DATEVALUE(REPLACE(LEFT(A1, 11), 7, 0, ","))+TIMEVALUE(RIGHT(A1, 8))
Note that the original in A1 is left-aligned; this indicates a text value. The converted date/time in B1 is right-aligned; this indicates a true date/time value.
Column B was formatted as dd/mm/yyyy hh:mm:ss. As General it would show as 42526.33333.

Using this value "2016-05-12 21:51:13 -0500" in Excel

How do I work with a Date Time -0500 value?
I have a sheet that has a value that looks like this:
2016-05-12 21:51:13 -0500
I want to be about to use it.
I want to filter all records that are greater than
2016-05-12 00:00:01 -0500
But I do not know how to work with this value.
Use this formula:
=--LEFT(A1,LEN(A1)-5)
Then format it like this
yyyy-mm-dd hh:mm:ss -\0\5\0\0
Then you can copy and paste the values and formatting where you want it
You need to convert the data into Excel date/times. With data in A1 in B1 enter:
=DATE(LEFT(A1,4),MID(A1,6,2),MID(A1,9,2))+TIME(MID(A1,12,2),MID(A1,15,2),MID(A1,18,2))
and format to display both date and time:
Let's presume for a moment that for some unknown reason Excel could not identify your string as a valid date time. You can always go back to basics and break your string down into its components. Let's start off and assume that your date time and offset string are in cell A1.
Step 1) Strip out the year
=left(A1,4)
Step 2) Strip out the month
=MID(A1,FIND("-",A1)+1,FIND("-",A1,FIND("-",A1)+1)-FIND("-",A1)-1)
That bad boy of a formula looks for the first - and the second - and based on that information finds the starting position to start pulling characters from and figures out how many characters to pull.
In your case we could have set it to pull two characters and had it start at character six as there is no variation to your date format. However, in a generic sense where there are not always leading zeros in the month, or sometimes you were flipping between four characters for the year and two characters for the year, the above would still work.
I am also assuming that month is the middle value (05) and you are not talking about December 5th here.
Step 3) Pull out the day
We could have used a similar approach using mid here, and again we could have hard coded it (wait, I did hard code two character return). Instead for a little flavour I used a right left sequence.
=RIGHT(LEFT(A1,FIND(" ",A1)-1),2)
Step 4) Pull out the time
Now you could go through the whole process and pull out hours, minutes and seconds, but Excel is usually pretty good at recognizing a time format as there is not much variation to it. Also this gives an opportunity to see a new formula for dealing with string manipulation.
Now since your time format was constant, I got a little lazy knowing that your time was always going to be eight characters long since your format always has a leading zero. As such, I used the following:
=TIMEVALUE(MID(A1,FIND(" ",A1)+1,8))
Basically, I grabbed the whole time, HH:mm:ss, and dumped it into timevalue (note there is also a datevalue). Timevalue will attempt to convert a string in time format to Excel time format as a decimal value.
Now as previously noted, if all those times are all stamped with the same -0500, just ignore it.
To get all that date and time converted into a single cell we would take each of the date parts and drop them into the DATE function and then add the time component on. In Excel speak that looks like:
=DATE(LEFT(A1,4),MID(A1,FIND("-",A1)+1,FIND("-",A1,FIND("-",A1)+1)-FIND("-",A1)-1),RIGHT(LEFT(A1,FIND(" ",A1)-1),2))+TIMEVALUE(MID(A1,FIND(" ",A1)+1,8))
Now if you want that to display with the -0500, look at Scott's answer for formatting. If you want to convert the time to local time and get rid of the -0500 then you would need to add -5 hours to the above formula which would look something like:
=DATE(LEFT(A1,4),MID(A1,FIND("-",A1)+1,FIND("-",A1,FIND("-",A1)+1)-FIND("-",A1)-1),RIGHT(LEFT(A1,FIND(" ",A1)-1),2))+TIMEVALUE(MID(A1,FIND(" ",A1)+1,8))+time(-5,0,0)
And if we were not so lazy and did not want to hard code the time, it would look more like:
=DATE(LEFT(A1,4),MID(A1,FIND("-",A1)+1,FIND("-",A1,FIND("-",A1)+1)-FIND("-",A1)-1),RIGHT(LEFT(A1,FIND(" ",A1)-1),2))+TIMEVALUE(MID(A1,FIND(" ",A1)+1,8))+TIME(LEFT(RIGHT(A1,4),2),RIGHT(A1,2),0)*IF(LEFT(RIGHT(A1,5),1)="-",-1,1)

Excel date formatting convert to number

I have a data file imported where there is a column for the date. It is custom formatted as [-$409]ddmmmyyyy h:mm:ss:000 so that it takes a number like 41769.68 and has it shown in the cell as 10May2014 16:23:04.883, for instance.
I want to be able to take just the hour and use that for conditional formatting though. For example, if the hour is between 7AM and 11AM, I want another column to say 'morning', or something of that sort. I know I can change the format cells option to just 'h' to get the hour, but using that in another block of code still returns the original 41749.68 number rather than 16, for example.
Is there a way to get just the hour to show in its actual number form rather than the whole date, or if not, is there a way to parse the number 41769.68 to find the hour?
The hour is .68 (the decimal part) *24 (each day is 1 unit to Excel). If you don't want the minutes then either round or truncate (eg with INT). Either way Conditional Formatting will not allow you to format as 'morning'.
You could try:
=HOUR(A1)
With your value in A1, in another cell enter:
=INT(24*(A1-INT(A1)))
will display 16

Resources