Text String to time

Mark McInerney

Active Member
Joined
Apr 4, 2012
Messages
288
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm working with a range of cells where the client has entered times as text strings and all into the one cell e.g. 9.00-14.00, 15.00-21.00

They are working on a 24 hour time with 2pm entered as 14.00

is it possible to extract the start time and the end time and covert them into times that I can use in other formulas?

Thanks for any help - appreciated - Mark.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
can you give some examples of how the dates are entered , all possible formats , you say 14:00 and also 2pm
do they always use a -
if so then textbefore and text after
=TIMEVALUE(TEXTBEFORE(A2,"-"))
=TIMEVALUE(TEXTAFTER(A2,"-"))

If its has PM or AM - then need substitute to add a space after the number and AM / PM
if a mixture of both , i'm not sure the best way - i have quite a complicated substitution
=TIMEVALUE(IF(ISNUMBER(SEARCH("AM",TEXTBEFORE(A3,"-"))),SUBSTITUTE(UPPER(TEXTBEFORE(A3,"-")),"AM"," AM"),SUBSTITUTE(UPPER(TEXTBEFORE(A3,"-")),"PM"," PM")))
=TIMEVALUE(IF(ISNUMBER(SEARCH("AM",TEXTAFTER(A3,"-"))),SUBSTITUTE(UPPER(TEXTAFTER(A3,"-")),"AM"," AM"),SUBSTITUTE(UPPER(TEXTAFTER(A3,"-")),"PM"," PM")))

Book16
ABCDEF
1AM timePM time
209:00-14:000.3750.583333330.3750.58333333
39am-2pm0.3750.583333330.3750.58333333
Sheet1
Cell Formulas
RangeFormula
B2B2=TIMEVALUE(TEXTBEFORE(A2,"-"))
C2C2=TIMEVALUE(TEXTAFTER(A2,"-"))
B3B3=TIMEVALUE(IF(ISNUMBER(SEARCH("AM",TEXTBEFORE(A3,"-"))),SUBSTITUTE(UPPER(TEXTBEFORE(A3,"-")),"AM"," AM"),SUBSTITUTE(UPPER(TEXTBEFORE(A3,"-")),"PM"," PM")))
C3C3=TIMEVALUE(IF(ISNUMBER(SEARCH("AM",TEXTAFTER(A3,"-"))),SUBSTITUTE(UPPER(TEXTAFTER(A3,"-")),"AM"," AM"),SUBSTITUTE(UPPER(TEXTAFTER(A3,"-")),"PM"," PM")))
E2:E3E2=TIMEVALUE(IF(ISNUMBER(SEARCH("AM",TEXTBEFORE(A2,"-"))),SUBSTITUTE(UPPER(TEXTBEFORE(A2,"-")),"AM"," AM"),SUBSTITUTE(UPPER(TEXTBEFORE(A2,"-")),"PM"," PM")))
F2:F3F2=TIMEVALUE(IF(ISNUMBER(SEARCH("AM",TEXTAFTER(A2,"-"))),SUBSTITUTE(UPPER(TEXTAFTER(A2,"-")),"AM"," AM"),SUBSTITUTE(UPPER(TEXTAFTER(A2,"-")),"PM"," PM")))
 
Upvote 0
Hi etaf,

Thanks for taking a look - I have been playing around with what you sent me, and this formula appears to be getting me exactly what I need.

=(TIMEVALUE(REPLACE(TEXTAFTER(D6,"-"),FIND(".",TEXTAFTER(D6,"-")),1,":")

Thank you for your help - greatly appreciated.
 
Upvote 0
.. this formula appears to be getting me exactly what I need.

=(TIMEVALUE(REPLACE(TEXTAFTER(D6,"-"),FIND(".",TEXTAFTER(D6,"-")),1,":")
That isn't a valid formula. In any case it, even if you add the extra parentheses to make it one, it only extracts the end time when you asked for both.

Perhaps you could consider this fairly short one that extracts both times?

24 01 28.xlsm
DEF
5StartEnd
69.00-14.0009:0014:00
715.00-21.0015:0021:00
86.08-10.5906:0810:59
99.55-23.0109:5523:01
Times
Cell Formulas
RangeFormula
E6:F9E6=--TEXTSPLIT(SUBSTITUTE(D6,".",":"),"-")
Dynamic array formulas.
 
Last edited:
Upvote 0
If you have any intention of using an Excel Table, TextBefore and TextAfter is a safer bet.
TextSplit will give you a SPILL error.

Book1
ABC
1TextStartEnd
29.00-14.009:0014:00
315.00-21.0015:0021:00
Sheet1
Cell Formulas
RangeFormula
B2:B3B2=--SUBSTITUTE(TEXTBEFORE([@Text],"-"),".",":")
C2:C3C2=--SUBSTITUTE(TEXTAFTER([@Text],"-"),".",":")
 
Upvote 0
If your goal is to calculate the time worked, see the following.
Building on Peter's suggestion see below or use the same logic with Alex's suggestion.

Time 2023.xlsm
DE
2Time
39.00-14.005:00
415.00-21.006:00
56.08-10.594:51
69.55-23.0113:06
718.15-6.0011:45
2g
Cell Formulas
RangeFormula
E3:E7E3=LET(t,--TEXTSPLIT(SUBSTITUTE(D3,".",":"),"-"),INDEX(t,2)-INDEX(t,1)+(INDEX(t,1)>INDEX(t,2)))
 
Last edited:
Upvote 0
If your goal is to calculate the time worked, see the following.
Building on Peter's suggestion see below or use the same logic with Alex's suggestion.

Time 2023.xlsm
DE
2Time
39.00-14.005:00
415.00-21.006:00
56.08-10.594:51
69.55-23.0113:06
718.15-6.0011:45
2g
Cell Formulas
RangeFormula
E3:E7E3=LET(t,--TEXTSPLIT(SUBSTITUTE(D3,".",":"),"-"),INDEX(t,2)-INDEX(t,1)+(INDEX(t,1)>INDEX(t,2)))

A bit shorter:
=LET(t,TEXTSPLIT(SUBSTITUTE(D3,".",":"),"-"),MOD(INDEX(t,2)-INDEX(t,1),1))
 
Upvote 0
A bit shorter:
=LET(t,TEXTSPLIT(SUBSTITUTE(D3,".",":"),"-"),MOD(INDEX(t,2)-INDEX(t,1),1))
Wouldn't that do the same thing without the MOD()?
In any case, another shorter one.
(Remembering of course that these are not what the OP actually asked for)

24 01 28.xlsm
DHIJK
4Post
5#6#7#7 without MOD#8
69.00-14.0005:0005:0005:0005:00
715.00-21.0006:0006:0006:0006:00
86.08-10.5904:5104:5104:5104:51
99.55-23.0113:0613:0613:0613:06
Times (3)
Cell Formulas
RangeFormula
H6:H9H6=LET(t,--TEXTSPLIT(SUBSTITUTE(D6,".",":"),"-"),INDEX(t,2)-INDEX(t,1)+(INDEX(t,1)>INDEX(t,2)))
I6:I9I6=LET(t,TEXTSPLIT(SUBSTITUTE(D6,".",":"),"-"),MOD(INDEX(t,2)-INDEX(t,1),1))
J6:J9J6=LET(t,TEXTSPLIT(SUBSTITUTE(D6,".",":"),"-"),INDEX(t,2)-INDEX(t,1))
K6:K9K6=SUM(TEXTSPLIT(SUBSTITUTE(D6,".",":"),"-")*{-1,1})
 
Last edited:
Upvote 0
If the time spans midnight, our suggestions calculate the correct time. This was shown in posts 6 and 7

Time 2023.xlsm
DEFGHIJK
2Post
3#6#7#7 without MOD#8
49.00-14.005:005:005:005:00
515.00-21.006:006:006:006:00
66.08-10.594:514:514:514:51
79.55-23.0113:0613:0613:0613:06
818.15-6.00time spans midnight11:4511:45###################
2g
Cell Formulas
RangeFormula
H4:H8H4=LET(t,--TEXTSPLIT(SUBSTITUTE(D4,".",":"),"-"),INDEX(t,2)-INDEX(t,1)+(INDEX(t,1)>INDEX(t,2)))
I4:I8I4=LET(t,TEXTSPLIT(SUBSTITUTE(D4,".",":"),"-"),MOD(INDEX(t,2)-INDEX(t,1),1))
J4:J8J4=LET(t,TEXTSPLIT(SUBSTITUTE(D4,".",":"),"-"),INDEX(t,2)-INDEX(t,1))
K4:K8K4=SUM(TEXTSPLIT(SUBSTITUTE(D4,".",":"),"-")*{-1,1})
 
Upvote 0
Time 2023.xlsm
DEFGHIJK
3#6#7#7 without MOD#8
49.00-14.005:005:005:005:00
515.00-21.006:006:006:006:00
66.08-10.594:514:514:514:51
79.55-23.0113:0613:0613:0613:06
818.15-6.00time spans midnight11:4511:4511:4511:45
2g
Cell Formulas
RangeFormula
H4:H8H4=LET(t,--TEXTSPLIT(SUBSTITUTE(D4,".",":"),"-"),INDEX(t,2)-INDEX(t,1)+(INDEX(t,1)>INDEX(t,2)))
I4:I8I4=LET(t,TEXTSPLIT(SUBSTITUTE(D4,".",":"),"-"),MOD(INDEX(t,2)-INDEX(t,1),1))
J4:J7J4=LET(t,TEXTSPLIT(SUBSTITUTE(D4,".",":"),"-"),INDEX(t,2)-INDEX(t,1))
K4:K8K4=SUM(MOD(TEXTSPLIT(SUBSTITUTE(D4,".",":"),"-")*{-1,1},1))
J8J8=LET(t,TEXTSPLIT(SUBSTITUTE(D8,".",":"),"-"),MOD(INDEX(t,2)-INDEX(t,1),1))
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top