I need know type variable TDateTime, TDate and TTime.
Anyone have any idea how to do this?
I used the code below, the result is "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.
Thanks
Delphi Basics : TDateTime command. The TDateTime type holds a date and time value. It is stored as a Double variable, with the date as the integral part, and time as fractional part. The date is stored as the number of days since 30 Dec 1899.
Delphi Basics : DaysBetween command. The DaysBetween function subtracts the FromDate from the ToDate, returning the number whole days difference. The time value of each date is taken account of - only whole 24 hour chunks are counted as whole days. A whole day does not have to start at 00:00:00.
The Implicit operator overloads of TValue got you.
When you assign the result of StrToDateTime, StrToDate and StrToTime to a TValue it uses the most matching Implicit operator overload from TValue which is Extended.
Also keep in mind that all three functions return TDateTime so even if there were operator overloads for TDateTime, TDate and TTime it would not work as expected.
To get the correct results you would have to explicitly specify the type when assigning your values to the TValue variables:
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' ));
Just in case you were trying to determine the result type of StrToDateTime:
type
TDateType = (dtDate, dtDateTime, dtTime);
function getDateType(date: TDateTime): TDateType;
begin
if Trunc(date) = date then // Or DateOf(date), if available
begin
Result := dtDate;
end
else
begin
if Trunc(date) = 0 then // Or DateOf(date), if avaialble
begin
Result := dtTime
end
else
begin
Result := dtDateTime;
end;
end;
end;
// Sample
var
result: TDateType;
begin
result := getDateType(StrToDateTime('01/01/2013 01:05:09')); // dtDateTime
result := getDateType(StrToDateTime('01/01/2015')); // dtDate
result := getDateType(StrToDateTime('01:01:02')); // dtTime
// One caveat
result := getDateType(StrToDateTime('01/01/2013 00:00:00')); // dtDate
end;
Alternatively, you could use the TryStrToDate, TryStrToTime, and TryStrToDateTime functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With