Is StrToDateTime broken in Delphi 11?

388 Views Asked by At

In Delphi 10.4 and older we could call StrToDateTime with custom format settings and the String would get parsed. In 11.3 this is not the case anymore.

I get the following exception:

Project X.exe raised exception class EConvertError with message ''2023-05-12 12:11:10' is not a valid date and time'.

Demonstration of the problem:

procedure ConvertDate;
var
  lFormatSettings: TFormatSettings;
  lDate: TDateTime;
begin
  lFormatSettings := TFormatSettings.Create();
  lFormatSettings.DateSeparator := '-';
  lFormatSettings.TimeSeparator := ':';
  lFormatSettings.ShortDateFormat := 'yyyy-mm-dd hh:nn:ss';

  lDate := StrToDateTime('2023-05-12 12:11:10', lFormatSettings);  // <-- EConvertError Exception
end;

Does someone know a workaround for this?

1

There are 1 best solutions below

10
Uwe Raabe On BEST ANSWER

You need to specify only the date part in ShortDateFormat. The time part goes into LongTimeFormat.

This is what works (at least here):

  lFormatSettings := TFormatSettings.Create();
  lFormatSettings.DateSeparator := '-';
  lFormatSettings.TimeSeparator := ':';
  lFormatSettings.ShortDateFormat := 'yyyy-mm-dd';
  lFormatSettings.LongTimeFormat := 'hh:nn:ss';