I suppose you are talking about conversion of a date string to a TDate. The record FormatSettings contains a list of country-specific settings, among them the element ShortDateFormat which, in my country, is the string "dd.mm.yy". This means: two-digit-day, then two-digit-month, then two-digit year, separated by dots. The FormatSettings are initialized when the program starts according to the defaults of the operating system (in Linux/Unix the unit "clocale" must be added to "uses").
The function StrToDate converts a string to a date value based on the ShortDateFormat string. I.e, the date string of today, '21.07.17' will be converted correctly in my country. You could also call TryStrToDate which returns a boolean false if the string cannot be converted successfully, instead of raising an exception.
Both functions are overloaded with versions accepting a local copy of the format settings to try other formats. The following code, for example, would check the date separators '.'. '/', and '-':
function MyStrToDate(s: String): TDate;
var
fs: TFormatSettings;
begin
// At first check default format
if TryStrToDate(s, Result) then
exit;
fs := FormatSettings;
// StrToDate replaces the slash ('/') by the DateSeparator of the FormatSettings.
if pos('/', FormatSettings.ShortDateFormat) > 0 then begin
if FormatSettings.DateSeparator <> '-' then begin
fs.DateSeparator := '-';
if TryStrToDate(s, result) then
exit;
end;
if FormatSettings.DateSeparator <> '/' then begin
fs.DateSeparator := '/';
if TryStrToDate(s, Result) then
exit;
end;
if FormatSettings.DateSeparator <> '.' then begin
fs.Dateseparator := '.';
if TryStrToDate(s, Result) then
exit;
end;
end;
// Now check cases where DateSeparator is hard-coded in ShortDateFormat
if FormatSettings.DateSeparator <> '-' then begin
fs.ShortDateFormat := StringReplace(FormatSettings.ShortDateFormat, FormatSettings.DateSeparator, '-', [rfReplaceAll]);
if TryStrToDate(s, Result) then
exit;
end;
if FormatSettings.DateSeparator <> '/' then begin
fs.ShortDateFormat := StringReplace(FormatSettings.ShortDateFormat, FormatSettings.DateSeparator, '/', [rfReplaceAll]);
if TryStrToDate(s, Result) then
exit;
end;
if FormatSettings.DateSeparator <> '.' then begin
fs.ShortDateFormat := StringReplace(FormatSettings.ShortDateFormat, FormatSettings.DateSeparator, '.', [rfReplaceAll]);
if TryStrToDate(s, Result) then
exit;
end;
// if we get here the string parts most probably do not lead to a valid date --> raise an exception
raise EConvertError.Create('Invalid date string ' + s)
end;
The function (Try)StrToDate, and thus the function above, is flexible enough to accept also a 4-digit year, or a 1-digit day or month, e.g. '21.7.2017'.
Therefore, I think that above function will cover most of the cases.