Platform SDK: International Features |
You can retrieve strings for any time or date in a format that is appropriate for the current locale by using the GetTimeFormat and GetDateFormat functions.
For GetTimeFormat, the time values in the SYSTEMTIME structure pointed to by lpTime must be valid. The function checks each of the time values to determine that it is within the appropriate range of values. If any of the time values are outside the correct range, the function fails, and sets the last-error to ERROR_INVALID_PARAMETER.
The function ignores the date portions of the SYSTEMTIME structure pointed to by lpTime: wYear, wMonth, wDayOfWeek, and wDay.
If a time marker exists and the TIME_NOTIMEMARKER flag is not set, the function localizes the time marker based on the specified locale identifier. Examples of time markers are "AM" and "PM" for US English, "de." and "du." for Mexican Spanish.
If TIME_NOMINUTESORSECONDS or TIME_NOSECONDS is specified, the function removes the separator(s) preceding the minutes and/or seconds element(s).
If TIME_NOTIMEMARKER is specified, the function removes the separator(s) preceding and following the time marker.
If TIME_FORCE24HOURFORMAT is specified, the function displays any existing time marker, unless the TIME_NOTIMEMARKER flag is also set.
The function does not include milliseconds as part of the formatted time string.
To use the LOCALE_NOUSEROVERRIDE flag, lpFormat must be NULL.
No errors are returned for a bad format string. The function simply forms the best time string that it can. If more than two hour, minute, second, or time marker format pictures are passed in, then the function defaults to two. For example, the only time marker pictures that are valid are L"t" and L"tt" (the 'L' indicates a Unicode (16-bit characters) string). If L"ttt" is passed in, the function assumes L"tt".
For GetDateFormat, the date values in the SYSTEMTIME structure pointed to by lpDate must be valid. The function checks each of the date values: year, month, day, and day of week. If the day of the week is incorrect, the function uses the correct value, and returns no error. If any of the other date values are outside the correct range, the function fails, and sets the last-error to ERROR_INVALID_PARAMETER.
The day name, abbreviated day name, month name, and abbreviated month name are all localized based on the locale identifier. The function ignores the time portions of the SYSTEMTIME structure pointed to by lpDate: wHour, wMinute, wSecond, and wMilliseconds.
The flag DATE_USE_ALT_CALENDAR will always use the format defined as the default format for that alternate calendar, rather than using any user overrides. The user overrides will only be used in the event that there is no default format for the alternate calendar specified.
The DATE_SHORTDATE and DATE_LONGDATE flag options are mutually exclusive. If neither one is specified and lpFormat is NULL, then DATE_SHORTDATE is the default.
If the lpFormat parameter is not NULL, the dwFlags parameter must be zero. None of the flags make sense when the format string is specified.
No errors are returned for a bad format string. The function simply forms the best date string that it can. For example, the only year pictures that are valid are L"yyyy" and L"yy" (the 'L' indicates a Unicode (16-bit characters) string). If L"y" is passed in, the function assumes L"yy". If L"yyy" is passed in, the function assumes L"yyyy". If more than 4 date (L"dddd") or 4 month (L"MMMM") pictures are passed in, then the function defaults to L"dddd" or L"MMMM".
Any text that should remain in its exact form in the date string should be enclosed within single quotes in the date format picture. The single quote may also be used as an escape character to allow the single quote itself to be displayed in the date string. However, the escape sequence must be enclosed within two single quotes. For example, to display the date as "May '93", the format string would be: L"MMMM ''''yy " The first and last single quotes are the enclosing quotes. The second and third single quotes are the escape sequence to allow the single quote to be displayed before the century.
To obtain the short and long date format for the default locale calendar, use the GetLocaleInfo function with the LOCALE_SSHORTDATE or LOCALE_SLONGDATE parameter. To get the date format for an alternate calendar, use GetLocaleInfo with the LOCALE_IOPTIONALCALENDAR parameter. To get the date format for a particular calendar, use GetCalendarInfo. Also, to return all of the date formats for a particular calendar, use EnumCalendarInfo or EnumDateFormatsEx.
To obtain the time format without performing any actual formatting, use the GetLocaleInfo function with the LOCALE_STIMEFORMAT.