Mirror

How to set the DisplayFormat of a TDateTime field to time only at runtime (Views: 704)


Problem/Question/Abstract:

I'm running a simple query that returns a variable amount of columns somtimes with a DateTime column. How can I set at runtime the Displayformat property or any other way to format the column as a time only field. In other words, I can't seem to find where to set the DisplayFormat property at runtime.

Answer:

Here's one way:

procedure FormatDateFieldsAsTime(DS: TDataSet; TimeFormat: string);
var
  f: integer;
begin
  for f := 0 to DS.FieldCount - 1 do
    if DS.Fields[f] is TDateTimeField then
      TDateTimeField(DS.Fields[f]).DisplayFormat := TimeFormat;
end;

Apply this to the query after it's been run, like:

FormatDateFieldsAsTime(Query1, 'hh:mm:ss');

The DisplayFormat is available (assuming you're not creating any persistant fields for this dynamic query) from the query's (or other TDataSet's) Fields property.

<< Back to main page