The problem with date formatting in Delphi is that Delphi assumes that the date separator character is only one character, for example:
"/" (e.g. English - United States)"." (e.g. French - Canada)
This does not work in locales that use more than one character for a date separator:
". " (e.g. Slovak - Slovakia)
In this case, Delphi when prompted by Windows for a date separator character:
GetLocaleInfo(LOCALE_SDATE, ...)
crashes and instead hardcodes the use of / .
To correctly convert a date to a string in Delphi, you must use the Windows API function designed to convert the date to a string, GetDateFormat :
function DateToStrW(const Value: TDateTime; const Locale: LCID=LOCALE_USER_DEFAULT): WideString; var pf: PWideChar; cch: Integer; TheDate: SYSTEMTIME; DateStr: WideString; begin //Code is released into the public domain. No attribution required. SysUtils.DateTimeToSystemTime(Value, TheDate); cch := Windows.GetDateFormatW(Locale, DATE_SHORTDATE, @TheDate, nil, nil, 0); if cch = 0 then RaiseLastWin32Error; SetLength(DateStr, cch); cch := Windows.GetDateFormatW(Locale, DATE_SHORTDATE, @TheDate, nil, PWideChar(DateStr), Length(DateStr)); if (cch = 0) then RaiseLastWin32Error; SetLength(DateStr, cch-1); //they include the null terminator /facepalm Result := DateStr; end;
source share