Re: Getting current date and time

[ Related Articles and Replies ] [ DelphiLand Discussion Forum ]

Posted by webmaster Guido on November 15, 2002 at 03:31:50:

In Reply to: Getting current date and time posted by Lesley Gousmett + P12256 on November 12, 2002 at 21:26:31:

: I have a TDBText component on my form which is a persistent database field and is named "dbStartTime". The Datasource is called "dsHire" and the datafield is "WhenStart". I want "dbStartTime" to display the current date and time, when the user clicks a button on the form called "New Hire". Can you tell me how I can do this?

You probably mean: a TDBText component which *shows the value of* a persistent database field and *the TDBText component* is named "dbStartTime". You don't mention the name of the persistent field, so let's assume that it is named "tableHireWHENSTART", the default name that Delphi creates if you use a TTable component with the name "tableHire".

Note that the persistent field does not display anything, because a field is a non-visual object. The displaying is done by the TDBText (or you might show the date in a TDBGrid, in a TDBEdit or other component). To clearify things, let's look at an example.

1. There is a physical table, say "Hire.dbf" (or a Paradox table "Hire.db" or whatever kind of table). One of its fields is a date-field, named "WhenStart".

2. Components on the Delphi form:

- a TTable called "tableHire", connected to the physical table.
- a TDataSource component called "dsHire", property "DataSet" set to "tableHire".
- a TDBText component called "dbStartTime", property DataSource set to dsHire, and property DataField set to "WhenStart".
- a button "btnNewHire", for adding new records to the table. The date-field must be filled automatically with today's date (I assume that you want to fill the other fields by hand, either through a set of TDBEdit components, or with the aid of a TDBGrid).

3. You created persistent fields for the table, and Delphi gave the name "tableHireWHENSTART" to the date-field.

Below is the code for the button's OnClick event. Note that the names of the TDataSource nor the TDBText are not needed for this code:

procedure TForm1.btnNewHireClick(Sender: TObject);
  tableHireWHENSTART.Value := Date;

The first line puts the TTable component in "Append" mode.
The second line assigns the value of "Date" to the persistent field tableHireWHENSTART ("Date" is a function from Delphi's library that returns today's date, a value of the type TDateTime).

After a click on btnNew you see today's date in the TDBText; now it's in the persistent field, but not yet in the table. In order to save it in the table, you must "post" the record, after having filled the other fields (such as LastName, FirstName, and so on...). Posting a record can be done in various ways: in code by giving the command tableHire.Post, or by clicking the post-button of a TTableNavigator that is connected to the TDataSource; note that also moving to another row in a TDBGrid posts the record that is being appended or edited.

Related Articles and Replies:

[ DelphiLand Discussion Forum ]