Search code examples
delphidatetimenullunassigned-variable

Delphi: How to determine an empty TDateTime value


It seems that there is no way to assign NULL (or an "unassigned value") to TDateTime variables.

The only way I've imagined is using something like this:

function isNull(aDate : TDateTime) : boolean;
const NullDate = 0.0;
var aNullDate : TDatetime;
    ms : Int64;
begin
  aNullDate := NullDate;
  ms := MilliSecondsBetween(aDate,aNullDate);
  result := (ms = Int64(0));
end;

Is there anybody out who knows better solution what not overlaps 0 date value?

Are negative TDateTime values dangerous? (I mean, as a usable resource for above purpose)


Solution

  • As Andreas already wrote, the TDateTime type is actually double and thus not "nullable". I use

    const
      c_UnassignedDate = -693594;
    

    for a empty date value as this represents an impossible date of 00/00/0000. But for example DevExpress uses

    NullDate = -700000;
    InvalidDate = NullDate + 1;
    

    So there seems to be no agreed upon standard vale, you should pick one which suits your need.