我将日期时间存储为字符串:
"20120515 061115"
将会
"May 15, 2012 at 6 hour 11 minutes 15 second"
如何将其转换为日期时间?也许使用“CDate”功能?
的输入格式CDate
取决于您的系统语言环境,这使得它无法解析以固定格式出现的值。
但是,由于您的日期组件位于固定位置,最简单的方法是使用字符串函数从原始字符串中提取值:
Dim s As String
Dim d As Date
s = "20120515 061115"
d = DateSerial(Left(s, 4), Mid(s, 5, 2), Mid(s, 7, 2)) _
+ TimeSerial(Mid(s, 10, 2), Mid(s, 12, 2), Mid(s, 14, 2))
或者您可以在查询中通过解析日期然后重构它来执行此操作:
在此示例中,我的 UglyDate 如下所示: Wed Mar 17, 2010 10:07 PM
SELECT [Tbl_UglyDate].[UglyDate], Mid([UglyDate],5,3) AS M, Mid([UglyDate],9,2) AS D,
Mid([UglyDate],13,4) AS Y, Month("1 " & [M]) AS M2,
([M2] & "/" & [D] & "/" & [Y]) AS date1, CVDate([date1]) AS [date]
FROM [Tbl_UglyDate];