使用特定的字符串格式将字符串转换为Excel日期和时间

时间:2022-05-06 22:02:13

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

我有一个Excel / VBA宏,它列出了目录中的文件。

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

文件名格式为:yyyy-MM-dd @hh-mm-ss_ [description] .csv

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

例如: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)

然后我需要将文件名的第一部分放入Excel Date对象(包括时间戳)

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.

我找到了CDate()函数,但它没有采用任何“format”参数,并且Format()函数的工作方式错误,即将String转换为Date。

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

我想获取一个带有String和格式的函数,并返回一个Excel Date / Time对象。

Is there a function designed to do that ?

是否有专门设计的功能?

Thanks,

谢谢,

1 个解决方案

#1


3  

Try this out:

试试这个:

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

#1


3  

Try this out:

试试这个:

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