0

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?

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
kkrajnc
  • 3
  • 1

1 Answers1

0

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';
Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • I have already tried this. It doesn't solve my problem. – kkrajnc May 12 '23 at 13:14
  • Not sure what you tried, but I added the code working here. – Uwe Raabe May 12 '23 at 13:58
  • `(Try)StrToDateTime()`, `(Try)StrToDate()`, and `(Try)StrToTime()` have always parsed a date using `ShortDateFormat` and `DateSeparator`, and parsed a time as fixed `hh[:mm[:ss[.zzz]]] [am|pm]` using the `TimeSeparator`, `DecimalSeparator`, and `Time(AM|PM)String` only, they do not use either `ShortTimeFormat` or `LongTimeFormat` at all when parsing a time. If parsing has recently stopped working, then that is a regression that needs to be [reported to Embarcadero](https://quality.embarcadero.com) – Remy Lebeau May 12 '23 at 16:21
  • "*they do not use either `ShortTimeFormat` or `LongTimeFormat` at all when parsing a time*" - however, when parsing a date (not a time), if `SysLocale.FarEast` is true and `ShortDateFormat` contains `'dddd'` then `ShortTimeFormat[1]` is checked for `['0'..'9']` and if so then any non-digit text following the date is skipped. – Remy Lebeau May 12 '23 at 16:29
  • In fact, `ShortTimeFormat` (and `LongTimeFormat`) are not used for any parsing at all (except for the 1 condition mentioned above), they are used only for formatting date/time strings. – Remy Lebeau May 12 '23 at 16:34
  • It may indeed be sufficient to set ShortDateFormat as shown and leave the time format as is. – Uwe Raabe May 12 '23 at 20:33
  • @Remy Lebeau: "If parsing has recently stopped working, then that is a regression that needs to be reported to Embarcadero". Assuming that it's the exact same problem, it is and it was, and their answer was "We had "several" bug reports about "bad" checking of formats. So, we fixed this. Earlier or later we have to pay our debts ...". See [RSP-35691](https://quality.embarcadero.com/browse/RSP-35691) – Philip J. Rayment May 13 '23 at 12:17
  • The effective point here is: It shouldn't have been working before. – Uwe Raabe May 13 '23 at 12:52
  • @kkrajnc Per [RSP-35691](https://quality.embarcadero.com/browse/RSP-35691), have you tried using `ShortDateFormat = 'yyyy/mm/dd'` with `DateSeparator = '-'`? – Remy Lebeau May 13 '23 at 17:08
  • @RemyLebeau I've tried now your suggestion and it works – kkrajnc May 17 '23 at 09:07