Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

Convert Text to Date in Excel Problems

So I open excel, in G2 put this: Feb/12/2016 01:09:28 PM UTC

Then in any other cell try this formula:

=IFERROR(DATEVALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LEFT(G2,11),"Dec",12),"Nov",11),"Oct",10),"Sep",9),"Aug",8),"Jul",7),"Jun",6),"May",5),"Apr",4),"Mar",3),"Feb",2),"Jan",1))+TIMEVALUE(MID(G2,13,11)),"Error")

I am trying to get it to show like 2/12/2016 13:09

But its giving me my error. Iv tried everything, just not working??

Many thanks guys

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Short answer:

Use the following formula:

=DATE(MID(A1,8,4),MONTH(1&LEFT(A1,3)),MID(A1,5,2)) + IF(MID(A1, 22,2) = "PM", TIME(MID(A1, 13, 2), MID(A1, 16, 2), MID(A1, 19, 2)) + TIME(12, 0, 0), TIME(MID(A1, 13, 2), MID(A1, 16, 2), MID(A1, 19, 2)))


Long answer:

While not necessary, it's probably better to separate Time and Date for data manipulation.

What I'd recommend is to use hidden columns, this makes your date nice and neatly separated, but furthermore will also help me explaining the answer rather than just pasting a giant formula:

Under presumption your text information always has the same format "Mon"/dd/yyyy hh:mm:ss AM/PM UTC

In cell B1, we'll paste our date:

=DATE(MID(A1,8,4),MONTH(1&LEFT(A1,3)),MID(A1,5,2))

Pretty self explanatory, we'll extract individual parts of the date format and apply it to them. The only trick part is using the MONTH()function to convert the String of Month to an actual Month format.


Now to the time, a bit more tricky:

Let's try to parse it in C1:

First, we'll need to implement an IF function, that will convert AM/PM format to 24h format and then we'll use string functions, similarly like in date above to extract data to our TIME() funciton.

=IF(MID(A1, 22,2) = "PM",  TIME(MID(A1, 13, 2), MID(A1, 16, 2), MID(A1, 19, 2)) + TIME(12, 0, 0), TIME(MID(A1, 13, 2), MID(A1, 16, 2), MID(A1, 19, 2)))

Last but not least, in the column D we'll paste our result. First make sure your format is set properly to your expected output:

enter image description here

and then we'll simply add our two data fields together:

=B1 + C1

Produces the expected result:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...