为什么TFormatSettings没有按预期运行?

Nic*_*ges 14 delphi str-to-date

我希望以下代码可以工作:

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.
Run Code Online (Sandbox Code Playgroud)

为什么会抛出异常,说"20121219"不是有效日期?这不正是通过TFormatSettings应该做的事情吗?

bum*_*mmi 20

StrToDate()需要在FS.DateSeparator:Char中定义的分隔符; 所以不能空着.

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