使用StrToDateTime和TFormatSettings进行转换不起作用

编程入门 行业动态 更新时间:2024-10-11 15:23:59
本文介绍了使用StrToDateTime和TFormatSettings进行转换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这段代码应该在Delphi XE2中工作,但是它在StrtoDateTime转换中给出了不是有效的日期和时间错误:

This code should work in Delphi XE2, but it gives "not a valid date and time" error in StrtoDateTime conversion:

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会询问操作系统是否不能自己确定。

If you want to convert some special DateTime-Formats you should better use VarToDateTime instead of StrToDateTime. Just have a look at the implementation of both and you will recognize, that StrToDateTime is somehow ... and VarToDateTime will ask the OS if it can't determine by itself.

这适用于Delphi XE3(但也应该与早期版本一起使用):

This works with Delphi XE3 (but should also work with earlier versions):

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 := VarToDateTime( s ); end;

更多推荐

使用StrToDateTime和TFormatSettings进行转换不起作用

本文发布于:2023-11-26 20:29:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635081.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   StrToDateTime   TFormatSettings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!