Why is TFormatSettings not behaving as expected?

我的梦境 提交于 2020-01-03 06:58:06

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/13961507/why-is-tformatsettings-not-behaving-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!