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.

This is sample code. Add error handling and adjust to your requirements as necessary.

#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
Picture Meaning
d Day of month as digits with no leading zero for single-digit days.
dd Day of month as digits with leading zero for single-digit days.
ddd Day of week as a three-letter abbreviation. The function uses the LOCALE_SABBREVDAYNAME value associated with the specified locale.
dddd Day of week as its full name. The function uses the LOCALE_SDAYNAME value associated with the specified locale.
M Month as digits with no leading zero for single-digit months.
MM Month as digits with leading zero for single-digit months.
MMM Month as a three-letter abbreviation. The function uses the LOCALE_SABBREVMONTHNAME value associated with the specified locale.
MMMM Month as its full name. The function uses the LOCALE_SMONTHNAME value associated with the specified locale.
y Year as last two digits, but with no leading zero for years less than 10.
yy Year as last two digits, but with leading zero for years less than 10.
yyyy Year 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.
yyyyy Identical to "yyyy".
gg Period/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>
No votes yet