Using timestamp in different fields

Asked

Viewed 1,047 times

2

I have a timestamp field in a postgres table. I would like to treat, in Delphi, the date in one dbedit and the time in another, is there such a possibility? In the tests I performed, I can change the values but when I quit, for example dbedit q keeps the date, it changes the time and the same happens when I quit dbedit time, it changes the date.

2 answers

1

One of the ways to do this is to not use Dbware components, and use Dataset events AfterScroll and BeforePost to update your fields that are not Dbware.

Considering the field DataHoraBanco you can do the following:

Create 2 fields without using DB components (TEdit, TDateTimePicker, TMaskEdit, etc.)

dteDataForm
edtHoraForm

On your dataset at the event AfterScroll fill in:

procedure Form1.DataSetonAfterScroll(DataSet: TDataSet);
begin
  dteDataForm.Date := Trunc(DataSet.FieldByName('DataHoraBanco').AsDateTime );
  edtHoraForm.Text := FormatDateTime('hh:nn:ss', DataSet.FieldByName('DataHoraBanco').AsDateTime);
end;

And at the event BeforePost do:

procedure Form1.DataSetonAfterScroll(DataSet: TDataSet);
begin
  DataSet.FieldByName('DataHoraBanco').AsDateTime := dteDataForm.Date +
    StrToTime(edtHoraForm.Text);
end;

Remembering that the Tdatetime type of Delphi is a Double and therefore the Trunc function extracts the whole part (date) and in the sum, the signal is used + to add up the decimal part equivalent to minutes and seconds.

0

In your Select you can create two "virtual" fields, one for time and one for date. Making a cast for date and another for time, from the original field(timestamp). In the Beforepost component event you concatenate the two values to the original field.

Example:

Select Cast(CampoTimeStamp as Date) as Campo_Data, Cast(CampoTimeStamp as Time) as Campo_Hora, CampoTimeStamp From...

No event before post would be something like this:

DataSet['CampoTimeStamp'] := DataSet['Campo_Data'] + DataSet['Campo_Hora'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.