Delphi - Why is TDate and TDateTime the same?

5.4k Views Asked by At

when looking in System.pas there TDate und TDateTime defined as the following:

  TDateTime = type Double;

  TDate = type TDateTime;
  TTime = type TDateTime;

obviously TDate and TDateTime are the same.

I just struggled working with TDate and TDateTime bacause I expected that TDate only contains the date-part and not also the time-part.

Now I'm wondering: What the sense behind this? When I declare a variable as TDate, then It should contain a date, and not a dat and a time-value.

1

There are 1 best solutions below

4
On

They are not the same. If the declarations had been

TDate = TDateTime;
TTime = TDateTime;

they had been the same. With the additional type, although they are still technically the same, i.e., they are still doubles, they can be told apart. This, for instance, makes it possible to use different property editors in the Object Inspector for the two types (a date picker and a time picker, respectively), while you may use a 'date-time' picker for TDateTime.

In addition, even if it weren't for this, it might be good to use different 'aliases' for different purposes. This might make your source code easier to understand. For instance, if you do

var
  StartTime: TDate;

then you know that StartTime contains only information about the date, and not the time (unless you abuse the norms).