3

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;
Ken White
  • 123,280
  • 14
  • 225
  • 444
Alberto
  • 2,881
  • 7
  • 35
  • 66
  • 1
    `fieldValue.AsType` – TLama Jul 02 '14 at 09:41
  • 1
    Please stop adding "In Delphi" in both the text and title of your questions. The `delphi` tag lets everyone know your question is about Delphi; it's not necessary to add it repeatedly all over the place. Tags here work very well, both to get your question in front of people who can answer it and to classify it properly for searches; it doesn't need the help of repetition. :-) Thanks. – Ken White Jul 04 '14 at 23:51

1 Answers1

6

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.

Stefan Glienke
  • 20,860
  • 2
  • 48
  • 102