This week I received an email from Lori who was having
some trouble working with data she downloaded from their
new computer system. Lori is with the sheriff's
department and one of the reports she prepares
calculates elapsed time between 'dispatch time' and
'on-scene time'. Problem is, the data she is getting
from their new system has the times formatted in
6-character text strings and are separate from the
dates.
In this example, you can see that the first dispatch
time (cell B2) is 5:41:13 PM and the on-scene time (cell
D2) is 5:47:57 PM.
Here is the solution I suggested for Lori...
Step 1: Extract the 'dispatch time' from the text
string
To convert a 6-digit text string to a time value you
have to extract each part (hours, minutes, seconds) and
put them into a TIME function.
TIME(hour,
minute, second)
You could use LEFT(B2,2) to extract to
left 2 characters, MID(B2,3,2) to
extract the middle 2 characters and
RIGHT(B2,2) to extract the right 2
characters.
But,
personally, I prefer to use the MID function to
extract all of the parts from a text string. I think
it's simpler to understand because you are using the
same function for all parts.
So for the first CALL DISPATCH TIME use the formula…
=TIME(MID(B2,1,2),MID(B2,3,2),MID(B2,5,2))
(see
alternate time_formula below)
MID(B2,1,2): From
cell B2, start at the 1st character and extract 2
characters.
MID(B2,3,2):
From cell B2, start at the 3rd character and
extract 2 characters.
MID(B2,5,2):
From cell B2, start at the 5th character and
extract 2 characters.
This will give you TIME(17,41,13) or
5:41:13 PM in cell E2.
Step 2: Format the result as Time
Now, you will need to format this cell as Time.
From the Home tab, click the Number format dropdown,
click More number formats..., choose Custom
at the bottom of the Category list and type hh:mm:
ss in the Type field.
Step 3: Combine the date and time for elapsed time
that spans multiple days
When you are calculating elapsed times that could span
multiple days, you'll need to use both the date and time
in your calculation.
Simply add the date from cell A2 to the time formula in
E2 to get both the date and time into one cell.
=A2+TIME(MID(B2,1,2),MID(B2,3,2),MID(B2,5,2))
(see
alternate date_time_formula below)
Now you'll need to format that cell with a date &
time format. I used a custom number format dd-mmm-yyyy
hh:mm:ss.
If you prefer, you can use an AM/PM time format (e.g. mmm-yyyy
h:mm:ss AM/PM)
Step 4: Copy this formula to extract the 'on-scene'
date and time
Now you can copy the formula (cell E2) to get the result
for the 'on-scene' date and time.
You'll need to copy it two columns to the right (cell
G2) so the cell references in the formula will adjust to
refer to the date and time cells two columns to the
right
(C and D).
Formula in cell E2: =A2+TIME(MID(B2,1,2),MID(B2,3,2),MID(B2,5,2))
Same formula when copied to cell G2: =C2+TIME(MID(D2,1,2),MID(D2,3,2),MID(D2,5,2))
Step 5: Calculate elapsed time
Now, in column H, you can calculate the elapsed time
simply by subtracting the DISPATCH date and time (E2)
from the ONSCENE date and time (G2).
Give that cell a TIME format hh:mm:ss
and now you’ve calculated the elapsed time from the
DISPATCH to the ONSCENE in hours, minutes, and seconds.
Elapsed times greater than 24 hours
If your elapsed time could result in greater than 24
hours, you will need to modify the time format by
wrapping the hours code in square brackets.
[hh]:mm:ss
Alternate Formulas
After originally posting this tip, I received
the following suggestion from Microsoft Excel
MVP, Rick Rothstein with a
shorter version of my formulas above.
As for your latest “Excel in Seconds” tip
("Calculating Elapsed Time Extracted From Text
Strings"), here is a much shorter formula for
you to consider…
time_formula:
=0+TEXT(B2,"00\:00\:00")
The backward slashes are 'escape' characters
that tell the TEXT function that the next
character should be treated as just a
character without any meaning that TEXT might
normally apply to it. TEXT considers the colon
as a time meta-character which, if used with
0's (the meta-character for digits) would be
inconsistent and generate an error. By
'escaping' the colon, TEXT does not try to
interpret it… it simply places it in the
output as a character.
When you calculate the date/time combination
for use in calculating the elapsed time, you
can eliminate the addition of the 0…
date_time_formula:
=A2+TEXT(B2,"00\:00\:00")
|
|