Declare Function GetTimeFormat Lib "kernel32.dll" Alias "GetTimeFormatA" (ByVal Locale As Long, ByVal dwFlags As Long, lpTime As SYSTEMTIME, ByVal lpFormat As Any, ByVal lpTimeStr As String, ByVal cchTime As Long) As Long
GetTimeFormat formats a string to display a time according to a locale's settings. The time can be formatted using either a predefined format or a custom format specified in the parameter list. The string generated by this function can be used to present a more human-readable way to display a time.
If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns the size in bytes of the string placed into the variable passed as lpTimeStr.
When passing 0 for the lpFormat parameter, the expression CLng(0) must be used.
Const LOCALE_SYSTEM_DEFAULT = &H400
Const LOCALE_USER_DEFAULT = &H800
Const LOCALE_NOUSEROVERRIDE = &H80000000
Const LOCALE_USE_CP_ACP = &H40000000
Const TIME_NOMINUTESORSECONDS = &H1
Const TIME_NOSECONDS = &H2
Const TIME_NOTIMEMARKER = &H4
Const TIME_FORCE24HOURFORMAT = &H8
' This code is licensed according to the terms and conditions listed here.
' Display today's time first using a "regular" hours-minutes-
' AM/PM string and then using a "military hours" format (e.g., 8:27 AM is 0827 hours).
Dim today As SYSTEMTIME ' today's date and time
Dim timestr As String ' receives the formatted time string
Dim strlen As Long ' length of the buffer for the formatted time string
' Get today's date and time in the local time zone.
GetLocalTime today
' Make sufficient room in the buffer to receive the time string.
timestr = Space(255)
' Format today's time as hours-minutes-AM/PM.
strlen = GetTimeFormat(0, TIME_NOSECONDS, today, CLng(0), timestr, Len(timestr))
' Remove the empty space from the formatted time string.
timestr = Left(timestr, strlen)
' Display today's time using that format.
Debug.Print "It is currently "; timestr
' Now make sufficient room once again.
timestr = Space(255)
' Format today's time in the military-esque format.
strlen = GetTimeFormat(0, 0, today, "HHmm 'hours'", timestr, Len(timestr))
' Remove the empty space from the formatted string.
timestr = Left(timestr, strlen)
' Display today's time in aforementioned format.
Debug.Print "It is currently "; timestr
Go back to the alphabetical Function listing.
Go back to the Reference section index.
Last Modified: January 3, 2000
This page is copyright © 2000 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/gettimeformat.html