Re: Directly Initialize Delphi TDateTime variable

Posted by Jerry on August 11, 2007

In Reply to How Do I Directly Initialize A TDateTime Value posted by Warwick Weston Wright on August 10, 2007

: I want to know how to initialise A TDateTime value directly without without using the TryEncodeDate Function. I would asume that TryEncode probaby indirectly calls the constructor of the TDateTime.

TDateTime doesn't have a constructor, only classes have a constructor. TDateTime isn't a "class", it's a "type": a value of the type TDateTime just contains a number.

You initialize a variable of TDateTime just like you initialize any other variable, simply by assigning a value to it that is "assignment compatible" with the type TDateTime., i.e. a number or another TDateTime value. That can be done with TryEncodeDate, but you can also use a simple assignment as shown in the examples below, as long as there is a number or in a TDateTime value at the right side of the assignment:

D := 2.25;  // 1/1/1900 at 6:00 hours
D := Date;  // today
D := Date + 1; // tomorrow
D := EncodeDateDay(2007, 50); // the 50th day of 2007
D := EndOfAMonth(2007, 12); // just before newyear 2008

...and so on. For Delphi's date and time functions, have a look in Help for the unit DateUtils.

Related articles

       

Follow Ups