Recent

Author Topic: TbufDataSet, formating a date field  (Read 2219 times)

cpalx

  • Hero Member
  • *****
  • Posts: 693
TbufDataSet, formating a date field
« on: July 17, 2021, 08:02:56 pm »
Hello,

how could ia format a field date in TbufDataSet

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TbufDataSet, formating a date field
« Reply #1 on: July 17, 2021, 11:59:41 pm »
As with any other dataset: add a field to FieldDefs with the proper type (ftDate, ftTime, ftTimeStamp, ...). You can set it or read it back with Field.AsDateTime.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

cpalx

  • Hero Member
  • *****
  • Posts: 693
Re: TbufDataSet, formating a date field
« Reply #2 on: July 18, 2021, 07:35:03 pm »
@lucamar, i wanted to say:

formatdate like 'dd/mm/yyyy' not field type.

wp

  • Hero Member
  • *****
  • Posts: 9024
Re: TbufDataSet, formating a date field
« Reply #3 on: July 18, 2021, 08:03:23 pm »
Use the 'DisplayFormat' property of the field. You must cast the field to TDateField, TDateTimeField, or TTimefield to have access to this property.

The following code works with a TBufDataset, TDataSource and a TDBGrid on a form (linked in the usual way):

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   F: TField;
  5. begin
  6.   BufDataset1.FileName := 'data.db';
  7.   if not FileExists(BufDataset1.FileName) then
  8.   begin
  9.     BufDataset1.FieldDefs.Add('date', ftDate);
  10.     BufDataset1.CreateDataset;
  11.     BufDataset1.Open;
  12.     for i := 0 to 100 do
  13.       BufDataset1.AppendRecord([Random(365) + EncodeDate(2020, 1, 1)]);
  14.   end;
  15.   BufDataset1.Open;
  16.  
  17.   F := BufDataset1.FieldByName('date');
  18.   if F is TDateField then
  19.     TDateField(F).DisplayFormat := 'yyyy-mm-dd';
  20. end;  
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018