delphi – 使用StrToDateTime和TFormatSettings进行转换不起作用
发布时间:2020-12-15 10:08:07 所属栏目:大数据 来源:网络整理
导读:这个代码应该在Delphi XE2中工作,但它在StrtoDateTime转换中给出了“不是有效的日期和时间”错误: procedure TForm2.Button1Click(Sender: TObject);var s: string; d: TDateTime; FmtStngs: TFormatSettings;begin GetLocaleFormatSettings(GetThreadLocal
这个代码应该在Delphi XE2中工作,但它在StrtoDateTime转换中给出了“不是有效的日期和时间”错误:
procedure TForm2.Button1Click(Sender: TObject); var s: string; d: TDateTime; FmtStngs: TFormatSettings; begin GetLocaleFormatSettings(GetThreadLocale,FmtStngs); FmtStngs.DateSeparator := #32; FmtStngs.ShortDateFormat := 'dd mmm yyyy'; FmtStngs.TimeSeparator := ':'; FmtStngs.LongTimeFormat := 'hh:nn'; s := FormatDateTime('',Now,FmtStngs); d := StrToDateTime(s,FmtStngs); end; 任何提示? 解决方法
如果要转换一些特殊的DateTime格式,您应该更好地使用
VarToDateTime而不是StrToDateTime.只要看看两者的实现,你会认识到,StrToDateTime不知何故…而VarToDateTime会询问操作系统是否不能自己确定.
这适用于Delphi XE3(但也应该与早期版本一起使用): procedure TForm2.Button1Click( Sender: TObject ); var s: string; d: TDateTime; FmtStngs: TFormatSettings; begin GetLocaleFormatSettings( GetThreadLocale,FmtStngs ); FmtStngs.DateSeparator := #32; FmtStngs.ShortDateFormat := 'dd mmm yyyy'; FmtStngs.TimeSeparator := ':'; FmtStngs.LongTimeFormat := 'hh:nn'; s := FormatDateTime( '',FmtStngs ); d := VarToDateTime( s ); end; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |