Search code examples
delphireflectiontdatetimetvalue

Convert a TValue into TDateTime


How can I convert a TValue in TDateTime?

I see there are methods like AsInteger, AsDouble, etc. but nothing for TDateTime.

var
  c : TRttiContext;
  t : TRttiType;
  f : TRttiField;
  fieldValue : TValue;
  fieldDateValue : TDateTime;
begin
  c := TRttiContext.Create;
    t := c.GetType(Self.ClassType);
    for f in t.GetFields do begin
      fieldValue := field.GetValue(Self);
      //convert the TValue in TDateTime
    end;
end;

Solution

  • The concrete AsXXX methods (like AsInteger) are just shortcuts to some intrinsic types (and some are also implemented with optimizations for the specific type). But to get any type from a TValue you can use the AsType<T> method.