Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Excel Date and Time, with specific string format

Tags:

excel

vba

I've got an Excel/VBA macro which list files in a directory.

Filenames are in format : yyyy-MM-dd@hh-mm-ss_[description].csv

for instance : 2013-07-03@22-43-19_my-file.csv

I then need to get the first part of the filename into an Excel Date object (including a Timestamp)

I found the CDate() function, but it doesn't take any "format" parameter, and the Format() function works the wrong way, ie to convert a String to a Date.

I'd like to get a function with takes a String and a format, and returns an Excel Date/Time object.

Is there a function designed to do that ?

Thanks,

like image 997
adrien.pain Avatar asked Jul 03 '13 20:07

adrien.pain


People also ask

How do I convert string to time in Excel?

Convert text string to date and time In the blank cell, type this formula =DATE(LEFT(A1,4),MID(A1,5,2),MID(A1,7,2))+TIME(MID(A1,10,2),RIGHT(A1,2),0), and then press Enter key, if you need, you can apply this formula to a range.

How do I convert string to date in Excel in YYYY-MM-DD format?

Select a blank cell next to your date, for instance. I1, and type this formula =TEXT(G1, "yyyy-mm-dd"), and press Enter key, then drag AutoFill handle over the cells needed this formula. Now all dates are converted to texts and shown as yyyy-mm-dd format.


1 Answers

Try this out:

Function ParseDateTime(dt As String) As Date
   ParseDateTime = DateValue(Left(dt, 10)) + TimeValue(Replace(Mid(dt, 12, 8), "-", ":"))
End Function
like image 68
chuff Avatar answered Oct 01 '22 06:10

chuff