Ошибка при преобразовании даты, и времени из строки

При преобразовании даты, и времени из строки получаю ошибку
"'09:00 21.02.2020' is not a valid date and time."

function START: TDateTime;
var
  FS: TFormatSettings;
begin
  FS := TFormatSettings.Create;
  FS.DateSeparator := '.';
  FS.ShortDateFormat := 'DD.MM.YYYY';
  FS.TimeSeparator := ':';
  FS.ShortTimeFormat := 'HH:NN';
  Result := StrToDateTime('09:00 21.02.2020', FS);
end;

В чем может быть проблема?


Ответы (1 шт):

Автор решения: zed

Функция StrToDateTime ожидает, что вначале идёт дата, а потом время. Об этом написано в SysUtils.pas перед описанием функции:

{ StrToDateTime converts the given string to a date and time value. The
  string must contain a date optionally followed by a time. The date and
  time parts of the string must follow the formats described for the
  StrToDate and StrToTime functions. }

function StrToDateTime(const S: string): TDateTime; overload; inline;
function StrToDateTime(const S: string; const AFormatSettings: TFormatSettings): TDateTime; overload;

Соответственно, вызывать надо вот так:

Result := StrToDateTime('21.02.2020 09:00', FS);

Либо, надо парсить строку и конвертировать время и дату отдельно, функциями StrToTime и StrToDate.

→ Ссылка