gpt4 book ai didi

delphi - Delphi中如何知道类型变量是DateTime、Date和Time

转载 作者:行者123 更新时间:2023-12-03 15:21:08 24 4
gpt4 key购买 nike

我需要知道类型变量 TDateTime、TDate 和 TTime。

有人知道如何做到这一点吗?

我使用下面的代码,结果是“Is NOT TDateTime”,“Is NOT TDate”,“Is NOT Ttime”


program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.Rtti,
System.SysUtils;

var
DateTime, Date,Time: TValue;

begin

DateTime:= StrToDateTime( '01/01/2013 01:05:09' );
if ( DateTime.TypeInfo = System.TypeInfo(TDateTime) ) then
Writeln( 'Is TDateTime' )
else
Writeln( 'Is NOT TDateTime' );

Date:= StrToDate( '01/01/2015' );
if ( Date.TypeInfo = System.TypeInfo(TDate) ) then
Writeln( 'Is TDate' )
else
Writeln( 'Is NOT TDate' );

Time:= StrToTime( '01:01:02' );
if ( Date.TypeInfo = System.TypeInfo(TTime) ) then
Writeln( 'Is TTime' )
else
Writeln( 'Is NOT TTime' );

Readln;

end.

谢谢

最佳答案

TValue隐式运算符重载成功了。

当您将 StrToDateTimeStrToDateStrToTime 的结果分配给 TValue 时,它会使用最匹配的来自 TValue隐式运算符重载,它是扩展

另请记住,所有三个函数都会返回 TDateTime,因此即使 TDateTimeTDateTTime 存在运算符重载 它不会按预期工作。

要获得正确的结果,您必须在将值分配给 TValue 变量时显式指定类型:

DateTime := TValue.From<TDateTime>(StrToDateTime( '01.01.2013 01:05:09' ));

Date:= TValue.From<TDate>(StrToDate( '01.01.2015' ));

Time:= TValue.From<TTime>(StrToTime( '01:01:02' ));

关于delphi - Delphi中如何知道类型变量是DateTime、Date和Time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19165019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com