Search code examples
delphidatetimepicker

TDateTimePicker and Date


It's a simple question for a weird thing of DatetTimePicker from Delphi XE7.

I have this code...

procedure TForm1.Button1Click(Sender: TObject);
begin
  DateTimePicker1.Date:= Date;
  memo1.Lines.Add(FloatToStr(Date) + ' vs ' + FloatToStr(DateTimePicker1.Date));
end;

Today, 18 of March 2015, after I press the button the results I get is:

42081 vs 42081.846316956

If I press again after 5 minutes I get the same result. Why the values are not the same?


Solution

  • The Date() function truncates the decimal portion of the return value (sets the time portion to 0). So it returns the current date/time with only the date filled in.

    The TDateTimePicker.Date property setter only updates the date portion of the internal stored TDateTime, leaving the existing time intact. The TDateTimePicker.Date property getter returns the entire internal stored date/time, not the date by itself, as one would expect. So you are seeing the updated date + the original time as initialized by TDateTimePicker.