Yet another extract datetime from text field query

d101au

New Member
Joined
Nov 30, 2017
Messages
1
Hello,

I have a column of cells with the following text format, "2:03pm September 27th – 3:12pm September 27th".

I want to extract the two date-time combinations into separate columns in Excel date-time format to enable further processing (e.g. to calculate duration of the event and to sort event rows into date time order)

So far I have found methods online to break the text into two elements (assuming the above text is in cell A1):
Code:
=LEFT(A1,FIND(" – ",A1)-1)
extracts the first date-time combination

and

Code:
=RIGHT(A1,LEN(A1)-(FIND(" – ",A1)+4))
extracts the second date-time combination.

I can extract the day of the month as a number from the extracted date-time result using (assuming the result of the first date-time extraction was placed in cell A2):
Code:
=VALUE(MID(A2,LEN(A2)-3,2))

The month can be extracted as a number using:
Code:
=MONTH(DATEVALUE(MID(A2,FIND(" ",A2)+1,FIND(" ",A2,FIND(" ",A2)+1)-FIND(" ",A2))&" 1"))

And the time can be extracted in Excel format via:
Code:
=TIMEVALUE(SUBSTITUTE(LEFT(A2,FIND(" ",A2)-1),"pm"," pm"))

Let us assume the YEAR is the current Excel year.

Combining the day number + month number + current year + the time into a single cell in Excel datevalue format can be done (assuming the day number value is in cell A4, the month number in A5 and the time in A6 respectively) via:
Code:
=DATEVALUE("01"&"/"&TEXT(DATE(2000,A5,1),"mm"))+(A4-1)+A6

Is there a better/more elegant method of extraction to achieve the same result?

Ciao
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Welcome to the Board!

If you use VBA the SPLIT function could split the cell into chunks divided by each space

Code:
Option Explicit

Sub ExtractDates()

    Dim x
    x = Split(Range("A1").Value, " ")
    Range("B1").Value = DateValue(x(1) & " " & Val(x(2))) + CDate(x(0))
    Range("C1").Value = DateValue(x(5) & " " & Val(x(6))) + CDate(x(4))
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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