Using the DateTimePicker component with Time

1

I'm using the TDateTimePicker component.

I placed the following value in the FORMAT property: dd / MM / yyyy HH: mm: ss

Running in form if not "clicking" on component and changing values, example

21/09/2018 14:55:56 para 21/10/2018 20:55:56

When capturing (DateTimePicker2.DateTime) the value I have 10/21/2018 14:55:56 for some reason the Time "HH: mm: ss" the component does not update.

I found the code on the internet to put on DateTimePicker's OnChange:

var
 lEdit: TCustomEdit;
begin
 TDateTimePicker(Sender).DateTime := StrToDateTime(TCustomEdit(Sender));

But I have the following error compiling:

E2250 There is no overloaded version of 'StrToDateTime' that can be called with these arguments
    
asked by anonymous 21.09.2018 / 20:21

2 answers

1

Drop a TDateTimePicker and a TEdit on the form, and then write the event handlers as follows:

type
  TForm1 = class(TForm)
    DateTimePicker1: TDateTimePicker;
    Edit1: TEdit;
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    DateTimePicker1.DateTime:= StrToDateTime(Edit1.Text);
    // Você também pode usar TryStrToDateTime()
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DateTimePicker1.Format:= 'dd / MM / yyyy HH: mm: ss';
end;

end.

To get the error, the StrToDateTime () function expects a String while you pass TCustomEdit . You can write if Sender is TEdit

StrToDateTime(TEdit(Sender).Text);
    
23.09.2018 / 17:46
-1

Using the OnChange method it is very easy to update the time

 procedure TForm1.DateTimePicker1Change(Sender: TObject);
 begin
   DateTimePicker1.Time := Now;
 end;
    
21.09.2018 / 23:03