为什么TFormatSettings的行为不符合预期? [英] Why is TFormatSettings not behaving as expected?

查看:106
本文介绍了为什么TFormatSettings的行为不符合预期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

为什么抛出异常,说 20121219不是有效日期?那不是传递TFormatSettings应该做的到底吗?

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?

推荐答案

StrToDate()需要在FS.DateSeparator中定义的分隔符:Char;因此不能为空。

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

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

这篇关于为什么TFormatSettings的行为不符合预期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆