How to encode a datetime in delphi

function EncodeDateTime(const AYear: Word;
 const AMonth: Word;
 const ADay: Word;
 const AHour: Word;
 const AMinute: Word;
 const ASecond: Word;
 const AMilliSecond: Word): TDateTime;
See this example
uses
DateUtils;

var
  myDateTime : TDateTime;

begin

 //Your Code
 myDateTime := EncodeDateTime(2009, 11, 28, 14, 23, 12, 000);
 //Your Code


End;
Another option
uses
SysUtils;

var
myDateTime : TDateTime;
begin
 //Your Code
 myDateTime:= EncodeDate(2009,11,28)+EncodeTime(14,23,12,000);
 //Your Code    
end;
The second option works because the TDatetime It is stored as a Double (TDateTime = type Double;), with the date as the integral part (the EncodeDate function returns the integral), and time as fractional part.
The date part of the TDateTime represents the number of days that have passed since 12/30/1899. a TDateTime can be any date through 31 Dec 9999 (decimal value 2,958,465), TDateTime values can also be negative. The decimal value -693593 corresponds to 1 Jan 0001.
see theses examples
var
myDateTime : TDateTime;

Begin
myDateTime :=0; //represents 12/30/1899
myDateTime :=1; //represents 12/31/1899
myDateTime :=-1; //represents 12/29/1899
myDateTime :=-693593; //represents 01/01/0001
myDateTime := Now(); //assign the current date and time to myDateTime 

myDateTime:=Trunc(Now()); //Extract only the date part.

myDateTime:=Frac(Now()); //Extract only the time part.

myDateTime :=Now() + 1;// Add a day to the current datetime


End;

Comments

Popular posts from this blog

Delphi stringgrid Sample (Using for loop)

Reports Fast Report Delphi Example

How to save jpg image to database and then load it in Delphi using FIBplus and TImage?