我得到一个芬兰语日期字符串,如下所示:
29.7.2011 9:27
我试图将此字符串转换为 VB6 中的 Date 对象。我试过使用 Format 函数,但它似乎没有吞下日期字符串,或者我做错了什么。这些是我尝试过的一些方法:
theDate = Format(dateString, "General Date")
theDate = Format(dateString, "DD.MM.YYYY MM:HH")
有什么想法吗?谢谢。
最佳答案
与其自己手动解析字符串,这很容易出错,而且如果您必须处理多种日期格式会变得困惑,您可以调出 OLE 自动化库(VB6 在内部用于许多事情,包括类型转换)为您进行转换。它可以将 Windows 支持的任何日期/时间格式的字符串转换回原始的日期。
Full disclosure: I agree with the sentiment in Deanna's answer: in general, you should try to use an unambiguous date/time format when converting dates to and from strings, but if you cannot do this for some reason, the solution outlined here should be fairly robust, as long as you know ahead of time what specific format the incoming date string will be in.
下面是使用 VarDateFromStr 的 DateFromString 函数示例内部函数将格式化的日期/时间 String 转换为 Date。
示例用法
将芬兰语日期字符串转换为日期并显示:
MsgBox DateFromString("29.7.2011 9:27", fl_FI)
在我的机器上(美国英语设置),显示“7/29/2011 9:27 AM”,这是正确的日期和时间(7 月 29 日)。
代码
将下面的代码放入项目中的新模块(.bas 文件)中以使用它。该代码目前支持解析美国英语 (en_US) 和芬兰语 (fl_FI) 日期字符串,但您可以根据需要添加对更多语言环境的支持。参见 Locale IDs assigned by Microsoft 获取区域设置 ID 的完整列表。
Option Explicit
Public Enum LocaleIDs
en_US = &H409 ' English (United States)
fl_FI = &H40B ' Finnish
' [[ Add other Locale ID's here as needed ]] '
End Enum
Private Declare Function VarDateFromStr Lib "oleaut32.dll" ( _
ByVal psDateIn As Long, _
ByVal lcid As Long, _
ByVal uwFlags As Long, _
ByRef dtOut As Date) As Long
Private Const S_OK = 0
Private Const DISP_E_BADVARTYPE = &H80020008
Private Const DISP_E_OVERFLOW = &H8002000A
Private Const DISP_E_TYPEMISMATCH = &H80020005
Private Const E_INVALIDARG = &H80070057
Private Const E_OUTOFMEMORY = &H8007000E
'
' Converts a date string in the specified locale to a VB6 Date.
'
' Example:
'
' Convert a Finnish date string as follows:
'
' DateFromString("29.7.2011 9:27", fl_FI)
'
Public Function DateFromString(ByVal sDateIn As String, ByVal lcid As LocaleIDs) As Date
Dim hResult As Long
Dim dtOut As Date
' Do not want user's own settings to override the standard formatting settings
' if they are using the same locale that we are converting from.
'
Const LOCALE_NOUSEROVERRIDE = &H80000000
' Do the conversion
hResult = VarDateFromStr(StrPtr(sDateIn), lcid, LOCALE_NOUSEROVERRIDE, dtOut)
' Check return value to catch any errors.
'
' Can change the code below to return standard VB6 error codes instead
' (i.e. DISP_E_TYPEMISMATCH = "Type Mismatch" = error code 13)
'
Select Case hResult
Case S_OK:
DateFromString = dtOut
Case DISP_E_BADVARTYPE:
Err.Raise 5, , "DateFromString: DISP_E_BADVARTYPE"
Case DISP_E_OVERFLOW:
Err.Raise 5, , "DateFromString: DISP_E_OVERFLOW"
Case DISP_E_TYPEMISMATCH:
Err.Raise 5, , "DateFromString: DISP_E_TYPEMISMATCH"
Case E_INVALIDARG:
Err.Raise 5, , "DateFromString: E_INVALIDARG"
Case E_OUTOFMEMORY:
Err.Raise 5, , "DateFromString: E_OUTOFMEMORY"
Case Else
Err.Raise 5, , "DateFromString: Unknown error code returned from VarDateFromStr (0x" & Hex(hResult) & ")"
End Select
End Function
关于parsing - 将芬兰日期字符串解析为 VB6 中的日期类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7399164/