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

不羁岁月 提交于 2021-02-07 14:15:13

问题


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,


回答1:


Try this out:

Function ParseDateTime(dt As String) As Date
   ParseDateTime = DateValue(Left(dt, 10)) + TimeValue(Replace(Mid(dt, 12, 8), "-", ":"))
End Function


来源:https://stackoverflow.com/questions/17457870/convert-string-to-excel-date-and-time-with-specific-string-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!