2012-07-17 207 views
2

我有一个VB6的程序与此行:力日期美国格式,无论区域设置

strDate = Format(Date, "ddmmmyyyy") 

我需要它根据的Windows的英语文化背景以这种格式总是出来(美国):

17Jul2012

不幸的是,当文化被设置为别的,法语,例如,我得到这个:

17juil2012

有没有什么办法让日期格式总是使用英式美式格式?

+1

的可能重复的[EXCEL - 格式值(掩模)](http://stackoverflow.com/questions/8523017/excel-format-value-mask) – GSerg 2012-07-17 21:06:05

+1

尽管问题看起来完全不同,但底层问题是相同的 - 为特定区域设置值而不是当前系统区域设置。 [在这个链接的问题中找到一个函数](http://stackoverflow.com/a/8523219/11683)。 – GSerg 2012-07-17 21:07:38

+0

@GSerg +1为努力,但对于这个特定的问题,我喜欢[Antagony's answer](http://stackoverflow.com/a/11531263/15639)更好 – MarkJ 2012-07-18 11:28:52

回答

3

而不是约试图执行一个区域性特定格式混乱,为什么不只是硬编码月份名称为一个简单的函数是这样的:

Private Function GetEnglishDate(ByVal d As Date) As String 
    Dim monthNames 
    monthNames = Array("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec") 
    GetEnglishDate = Day(d) & monthNames(Month(d)) & Year(d) 
End Function 

用法:

strDate = GetEnglishDate(myDate) 
+0

谢谢,这是一个非常优雅的解决方案。 – 2012-07-18 14:04:40

1

使用内置的Windows日期格式功能:

Option Explicit 

Private Type SYSTEMTIME 
    wYear   As Integer 
    wMonth   As Integer 
    wDayOfWeek  As Integer 
    wDay   As Integer 
    wHour   As Integer 
    wMinute   As Integer 
    wSecond   As Integer 
    wMilliseconds As Integer 
End Type 

Private Declare Function GetDateFormat Lib "Kernel32" Alias "GetDateFormatW" (_ 
    ByVal Locale As Long, _ 
    ByVal dwFlags As Long, _ 
    ByRef lpDate As SYSTEMTIME, _ 
    ByVal lpFormat As Long, _ 
    ByVal lpDateStr As Long, _ 
    ByVal cchDate As Long _ 
) As Long 

Private Declare Function VariantTimeToSystemTime Lib "OleAut32.dll" (_ 
    ByVal vtime As Date, _ 
    ByRef lpSystemTime As SYSTEMTIME _ 
) As Long 

Private Sub Command_Click() 

    ' Use French Canadian date - should display "mer., juil. 18 12" for today! 
    Label.Caption = FormatDateWithLocale("ddd',' MMM dd yy", Now, 3084) 

    ' Use United States date - should display "Wed, July 18 12" for today! 
    Labe2.Caption = FormatDateWithLocale("ddd',' MMM dd yy", Now, 1033) 

End Sub 

Private Function FormatDateWithLocale(ByRef the_sFormat As String, ByVal the_datDate As Date, ByVal the_nLocale As Long) As String 

    Dim uSystemTime     As SYSTEMTIME 
    Dim nBufferSize     As Long 

    ' Convert to standard Windows time format. 
    If VariantTimeToSystemTime(the_datDate, uSystemTime) = 1 Then 

     ' Run "GetDateFormat" just to get the size of the output buffer. 
     nBufferSize = GetDateFormat(the_nLocale, 0&, uSystemTime, StrPtr(the_sFormat), 0&, 0&) 

     If nBufferSize > 0 Then 

      ' The buffer size includes the terminating null char, but all VB strings always include this, therefore allocate a buffer with one less character. 
      ' Then rerun the GetDateFormat. 
      FormatDateWithLocale = Space$(nBufferSize - 1) 
      GetDateFormat the_nLocale, 0&, uSystemTime, StrPtr(the_sFormat), StrPtr(FormatDateWithLocale), nBufferSize 

     End If 

    End If 

End Function 

只要使用不同的区域设置的数字(见http://www.dotnetindex.com/articles/990-List-of-Locale-ID--LCID--Values-as-Assigned-by-Microsoft.asp

的日期格式从VB那些稍微不同的(M为月):

http://msdn.microsoft.com/en-us/library/dd317787.aspx

相关问题