Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse GetDateFormat to format a date value using locale defaults or custom formatting
Keywordsformat currency, GetCurrencyFormat, locale
CategoriesStrings, Software Engineering
 
The GetDateFormat API function formats a date. This example lets you set the locale ID to LOCALE_SYSTEM_DEFAULT, LOCALE_USER_DEFAULT, or 0. If you set it to 0, the program lets you specify long or short date, a year/month format (e.g. November, 2003), and whether to use an alternate calendar.

GetDateFormat places the formatted result in a buffer and returns the length of the buffer.

 
' Format a numeric string.
Private Function FormatDate(system_time As SYSTEMTIME, _
    ByVal locale_id As Long, ByVal short_date As Boolean, _
    ByVal long_date As Boolean, ByVal year_month As _
    Boolean, ByVal alt_calendar As Boolean) As String
Dim flags As Long
Dim buf As String * 200
Dim buflen As Integer

    flags = 0
    If short_date Then flags = flags Or DATE_SHORTDATE
    If long_date Then flags = flags Or DATE_LONGDATE
    If year_month Then flags = flags Or DATE_YEARMONTH
    If alt_calendar Then flags = flags Or _
        DATE_USE_ALT_CALENDAR

    If locale_id = 0 Then
        ' Custom.
        ' See if we need to use a format.
        If (flags = 0) And Len(txtFormat.Text) > 0 Then
            buflen = GetDateFormat(locale_id, flags, _
                system_time, txtFormat.Text, buf, Len(buf))
        Else
            buflen = GetDateFormat(locale_id, flags, _
                system_time, ByVal 0&, buf, Len(buf))
        End If
    Else
        ' Locale.
        buflen = GetDateFormat(locale_id, 0, _
            system_time, ByVal 0&, buf, Len(buf))
    End If

    FormatDate = Left$(buf, buflen)
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated