Delphi: determining and deleting a TDatetime value

There seems to be no way to assign NULL (or an " unassigned value " to TDateTime variables.

The only way I imagined was to use 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 anyone who knows a better solution that doesn't overlap the date value 0?

Is TDateTime negative for TDateTime ? (As a capable resource for the previous goal)

+11
null datetime delphi
source share
4 answers

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

 const c_UnassignedDate = -693594; 

for an empty date value, as this means an impossible date 00/00/0000 . But, for example, DevExpress uses

 NullDate = -700000; InvalidDate = NullDate + 1; 

So it seems that there is no agreed standard valley, you should choose the one that suits you.

+13
source share

First you need to determine what you mean by "empty TDateTime ".

The value of A TDateTime is a double number with the date encoded in the integer part and the time encoded in the fractional part. So, the closest thing to a "zero date" that you can probably get is 0 .

Therefore, just test ADate <> 0 to check if the date is "null".

But be careful: if you declare a local variable, TDateTime , then it will not necessarily be =0 before you give it a value. It could be anything. Of course, the same applies to variables of type integer , double , boolean , ...

In addition, I believe that TDateTime with a value of 0 encodes the date 1899-12-30.

Finally, negative TDateTime values โ€‹โ€‹are perfectly normal. For example, -5000 corresponds to 1886-04-22 .

I do not quite understand the meaning of your code. If you want to use 0 as an โ€œunassignedโ€ value (which is bad if you are interested in dates close to 1899-12-30), why not just do

 function IsUnassigned(ADate: TDateTime): boolean; begin result := ADate = 0; end; 

or perhaps (but not equivalent!)

 function IsUnassigned(ADate: TDateTime): boolean; begin result := IsZero(Date); end; 

In his answer, ain gave a couple of more reasonable solutions for the meaning of "unrecognized date".

+9
source share

Use PDateTime instead of TDateTime and send as nil. If there is no pointer to a value, do not get the value.

To verify the use of the Assigned ptr value:

 MyDatePointer := nil; if ( Assigned(MyDatePointer)) then ... 
+1
source share

tDateTime is undefined for values โ€‹โ€‹from 0 to -1, which means that tDateTime -0.5 is undefined and is useful for date zero as an alternative to NaN. DateTimeToString will display -0.5 in the same way as +0.5;

+1
source share

All Articles