将芬兰语日期字符串解析为VB6中的日期类型 [英] Parse finnish date string to Date Type in VB6

查看:103
本文介绍了将芬兰语日期字符串解析为VB6中的日期类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的芬兰日期字符串如下:

29.7.2011 9:27

我正在尝试将此字符串转换为VB6中的Date对象.我尝试使用Format函数,但是它似乎没有吞下日期字符串,或者我做错了什么.这些是我尝试过的一些方法:

theDate = Format(dateString, "General Date")

theDate = Format(dateString, "DD.MM.YYYY MM:HH")

有什么想法吗?谢谢.

解决方案

您可以自己调出OLE自动化库(VB6在内部将其用于许多事情,包括类型转换)将为您完成转换.它可以将Windows支持的任何日期/时间格式的字符串转换回原始的日期.

完全公开:我同意DateFromString 函数的示例"rel =" nofollow noreferrer>内部的 VarDateFromStr 函数将格式化的日期/时间 String 转换为 Date .

用法示例

将芬兰日期字符串转换为日期并显示它:

MsgBox DateFromString("29.7.2011 9:27", fl_FI)

在我的机器上(美国英语设置),该屏幕显示"7/29/2011 9:27 AM",这是正确的日期和时间(7月29日).

代码

将下面的代码放入项目中的新模块(.bas文件)中以使用它.该代码当前支持解析美国英语( en_US )和芬兰语( fl_FI )日期字符串,但是您可以根据需要添加对更多语言环境的支持.有关完整的信息,请参见 Microsoft分配的区域设置ID 区域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

I'm getting a Finnish date string that looks like:

29.7.2011 9:27

I'm trying to cast this string to a Date object in VB6. I've tried using the Format function but it doesn't seem to swallow the date string or I'm doing something wrong. These are some approaches I've tried:

theDate = Format(dateString, "General Date")

theDate = Format(dateString, "DD.MM.YYYY MM:HH")

Any ideas? Thanks.

解决方案

Rather than manually parsing the string yourself, which is prone to errors, and which gets messy if you have to deal with multiple date formats, you can call out to the OLE Automation library (which VB6 uses internally for many things, including type conversions) to do the conversion for you. It can convert strings in any date/time format supported by Windows back into a raw Date.

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.

Below is an example of a DateFromString function that uses the VarDateFromStr function internally to convert a formatted date/time String into a Date.

Example Usage

Convert a Finnish date string to a Date and display it:

MsgBox DateFromString("29.7.2011 9:27", fl_FI)

On my machine (US English settings), this displays "7/29/2011 9:27 AM", which is the correct date and time (July 29).

Code

Place the code below into a new module (.bas file) in your project to use it. The code currently supports parsing US English (en_US) and Finnish (fl_FI) date strings, but you can add support for more locales if needed. See Locale IDs assigned by Microsoft for a complete list of locale ID's.


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

这篇关于将芬兰语日期字符串解析为VB6中的日期类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆