Formating a date as a date string for a locale specified

The GetDateFormat Windows API function formats a date as a date string for a Locale Identifier specified. The formatting is controlled either by flags or by the formatting string, if provided. The function accepts SYSTEMTIME Structure

as one of its parameters.

$SAMPLECODE$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#DEFINE LOCALE_USER_DEFAULT	0x400
#DEFINE DATE_LONGDATE		0x2
#DEFINE DATE_SHORTDATE		0x1
#DEFINE DATE_YEARMONTH		0x8

CLEAR

? "--- Windows user settings"
? GetFormatedDate(DATE(), LOCALE_USER_DEFAULT, DATE_LONGDATE)
? "--- Windows user settings - Custom format"
? GetFormatedDate(DATE()+1, LOCALE_USER_DEFAULT, "dddd dd")

? "--- English (United Kingdom)"
? GetFormatedDate(DATE(), 0x0809, DATE_LONGDATE)
? "--- English (United States)"
? GetFormatedDate(DATE(), 0x0409, DATE_LONGDATE)
? "--- English (Australia)"
? GetFormatedDate(DATE(), 0x0c09, DATE_LONGDATE)

* Parameters
* tdDate - date to be formatted
* tnLocale - Locale ID
* tvFlagsOrFormat - Either one of the flags (DATE_LONGDATE, DATE_SHORTDATE, DATE_YEARMONTH)
*                    or the formatting string. The Formatting codes are listed in a table below.)
FUNCTION GetFormatedDate(tdDate, tnLocale, tvFlagsOrFormat)
	LOCAL lcDate, lcFormat, lcDateStr, lnDateStrLen, lnFlags

	DO CASE
	CASE VARTYPE(tvFlagsOrFormat) = "N" 	
		lnFlags = tvFlagsOrFormat
		lcFormat = ""
		lcFormat = NULL
	CASE VARTYPE(tvFlagsOrFormat) = "C" 	
		lnFlags = 0 
		lcFormat = tvFlagsOrFormat
	OTHERWISE 
		ASSERT .F. MESSAGE "Missing or Invalid 3rd parameter."
	ENDCASE


	* SYSTEMTIME Structure. Only Year, Month and Day members are relevant. 
	lcDate = ;
		BINTOC(YEAR(tdDate), "2RS") + ;
		BINTOC(MONTH(tdDate), "2RS") + BINTOC(0, "2RS") + ;
		BINTOC(DAY(tdDate), "RS") + BINTOC(0, "8RS") 

	lnDateStrLen = 255
	lcDateStr = SPACE(lnDateStrLen)

	lnDateStrLen = GetDateFormat(tnLocale, lnFlags, lcDate, lcFormat, @lcDateStr, lnDateStrLen)
	lcDateStr = LEFT(lcDateStr, lnDateStrLen - 1)

	RETURN lcDateStr
ENDFUNC

FUNCTION GetDateFormat(Locale, dwFlags, lpDate, lpFormat, lpDateStr, cchDate)
	DECLARE Long GetDateFormat IN win32api AS GetDateFormat ;
		Long Locale, Long dwFlags, String lpDate, String lpFormat, String @lpDateStr, Long cchDate
	RETURN GetDateFormat(Locale, dwFlags, lpDate, lpFormat, @lpDateStr, cchDate)
ENDFUNC

Formatting codes
PictureMeaning
dDay of month as digits with no leading zero for single-digit days.
ddDay of month as digits with leading zero for single-digit days.
dddDay of week as a three-letter abbreviation. The function uses the LOCALE_SABBREVDAYNAME value associated with the specified locale.
ddddDay of week as its full name. The function uses the LOCALE_SDAYNAME value associated with the specified locale.
MMonth as digits with no leading zero for single-digit months.
MMMonth as digits with leading zero for single-digit months.
MMMMonth as a three-letter abbreviation. The function uses the LOCALE_SABBREVMONTHNAME value associated with the specified locale.
MMMMMonth as its full name. The function uses the LOCALE_SMONTHNAME value associated with the specified locale.
yYear as last two digits, but with no leading zero for years less than 10.
yyYear as last two digits, but with leading zero for years less than 10.
yyyyYear represented by full four or five digits, depending on the calendar used. Thai Buddhist and Korean calendars both have five digit years. The "yyyy" pattern will show five digits for these two calendars, and four digits for all other supported calendars.
yyyyyIdentical to "yyyy".
ggPeriod/era string. The function uses the CAL_SERASTRING value associated with the specified locale. This element is ignored if the date to be formatted does not have an associated era or period string. /td>

Comments