1

如果用户以 dd/mm/yyyy 格式输入日期为 2011 年 6 月 1 日 (01/06/2011)

转换后它返回“2011 年 1 月 6 日”而不是“2011 年 6 月 1 日”。

让我解释一下它是如何在 2011 年 1 月 1 日转换它的。

User entered = 01/06/2011 (dd/mm/yyyy)   i.e. 1st june 2011
After conversion it returns = 01/06/2011 (mm/dd/yyyy) i.e. 6th jan 2011

请记住:用户日期格式在设计时是未知的。它正在从数据库中获取并存储在变量(字符串)中

有没有什么解决办法。

请仅提供与 VB6 相关的解决方案,不在 .net 中

4

2 回答 2

0

您可以使用以下内容查找用户区域日期格式设置:

Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Private Const LOCALE_USER_DEFAULT = &H400
Private Const LOCALE_SSHORTDATE = &H1F ' short date format string
Private Const LOCALE_SLONGDATE = &H20 ' long date format string

Public Function GetUserShortDateFormat() As String
    Dim strLocale As String
    Dim lngRet As Long

    'Get short date format
    strLocale = Space(255)
    lngRet = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, strLocale, Len(strLocale))
    strLocale = Left(strLocale, lngRet - 1)

    GetUserShortDateFormat = strLocale
End Function

但这并不能保证用户实际上是以这种格式输入的。

但是,如果您将数据库日期列字段中的日期读入日期变量,它将采用这种格式。

于 2011-06-01T08:58:54.040 回答
0

很好,我已经编写了自己的方法来将日期分解为标记,因为我发现没有其他方法可以解决这个问题。

于 2011-12-29T09:21:41.640 回答