14

I expect the following code to work:

program Project3;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

var
  FS: TFormatSettings;

const
  DF = 'yyyymmdd';

begin
  try
   WriteLn(FormatDateTime(DF, Now));

   FS := TFormatSettings.Create;
   FS.ShortDateFormat := DF;
   WriteLn(StrToDate('20121219', FS));

   ReadLn;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Why is it throwing an exception, saying that '20121219' is not a valid date? Isn't that exactly what passing the TFormatSettings is supposed to do?

Danilo Casa
  • 505
  • 1
  • 9
  • 16
Nick Hodges
  • 15,957
  • 9
  • 59
  • 120

1 Answers1

20

StrToDate() needs the separator which is defined in FS.DateSeparator: Char; and so can't be empty.

Reference: http://docwiki.embarcadero.com/Libraries/XE3/en/System.SysUtils.StrToDate

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
bummi
  • 26,435
  • 13
  • 58
  • 97