Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare a TDateTime as a Const in Delphi

As far as I know there is no way to do this, but I am going to ask just in case someone else knows how to do this. How can I declare a date as a const in Delphi?

The only solution I have found is to use the numeric equivalent, which is kind of a pain to maintain because it is not human readable.

const   Expire : TDateTime = 39895; // Is actually 3/23/2009 

What I would like to be able to do is something like this:

const   Expire : TDateTime = TDateTime ('3/23/2009'); 

or

const   Expire : TDateTime = StrToDate('3/23/2009'); 

So let me know if this is a feature request or if I just missed how to do this (yeah, I know it seems like an odd thing to want . . . .)

like image 532
Jim McKeeth Avatar asked Mar 23 '09 23:03

Jim McKeeth


People also ask

How do you declare a date in Delphi?

Declaration: function IncDay(ADate: TDateTime; Days: Integer = 1) : TDateTime; Description: Adds or substracts a given number of days from a date value.


2 Answers

Ok, my reaction is a bit late, but here's a solution for the newer Delphi's.

It uses implicit class overloaders so that records of this type can be used as if they are TDateTime variables.

  TDateRec = record     year,month,day,hour,minute,second,millisecond:word;     class operator implicit(aDateRec:TDateRec):TDateTime;     class operator implicit(aDateTime:TDateTime):TDateRec; // not needed     class operator implicit(aDateRec:TDateRec):String; // not needed     class operator implicit(aDateRec:String):TDateRec; // not needed   end; 

Implementation:

uses DateUtils;  class operator TDateRec.Implicit(aDateRec:TDateRec):TDateTime; begin   with aDateRec do // Yeah that's right you wankers. I like "with" :)     Result := encodeDateTime(Year,Month,Day,Hour,Minute,Second,Millisecond); end;  class operator TDateRec.Implicit(aDateTime:TDateTime):TDateRec; begin   with Result do     DecodeDateTime(aDateTime,Year,Month,Day,Hour,Minute,Second,Millisecond); end;  class operator TDateRec.Implicit(aDateRec:TDateRec):String; begin   Result := DateTimeToStr(aDateRec) end;  class operator TDateRec.Implicit(aDateRec:String):TDateRec; begin   Result := StrToDateTime(aDateRec) end; 

Now you can declare your dates like this:

const   Date1:TDateRec=(Year:2009;month:05;day:11);   Date2:TDateRec=(Year:2009;month:05;day:11;hour:05);   Date3:TDateRec=(Year:2009;month:05;day:11;hour:05;minute:00); 

To see if it works, execute the following:

ShowMessage(Date1); // it can act like a string ShowMessage(DateToStr(Date1)); // it can act like a date 

If you really want to replace all your TdateTime variables with this, you probably need to overload some other operators too (Add, subtract, explicit, ...).

like image 59
Wouter van Nifterick Avatar answered Oct 05 '22 05:10

Wouter van Nifterick


The only? possible way, but probably not what you are looking for:

const {$J+}   Expire: TDateTime = 0; {$J-}  initialization   Expire := EncodeDate(2009, 3, 23); 
like image 26
The_Fox Avatar answered Oct 05 '22 06:10

The_Fox