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 GetTimeFormat to format a time value using locale defaults or custom formatting
Keywordsformat time, GetCurrencyFormat, locale
CategoriesStrings, Software Engineering
 
The GetTimeFormat 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 no minutes or seconds, no seconds, no time marker (AM/PM), and force 24-hour format.

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

 
' Format a numeric string.
Private Function FormatTime(system_time As SYSTEMTIME, _
    ByVal locale_id As Long, ByVal no_minutes_seconds As _
    Boolean, ByVal no_seconds As Boolean, ByVal _
    no_time_marker As Boolean, ByVal force_24_hour_format _
    As Boolean) As String
Dim flags As Long
Dim buf As String * 200
Dim buflen As Integer

    flags = 0
    If no_minutes_seconds Then flags = flags Or _
        TIME_NOMINUTESORSECONDS
    If no_seconds Then flags = flags Or TIME_NOSECONDS
    If no_time_marker Then flags = flags Or _
        TIME_NOTIMEMARKER
    If force_24_hour_format Then flags = flags Or _
        TIME_FORCE24HOURFORMAT

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

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