开发者

How to encode a datetime in delphi

I know how to use encodedate in Delphi to encode individual YY, MM and DD into a datetime fi开发者_运维问答eld or use encodetime to encode individual HH, SS, MM and MS into datetime field but is there a way to specify both date and time into a datetime field?

Coz with encodedate I cannot specify the time and with encodetime I cannot specify the date...

e.g. how can I set a datetime field to 2009-11-28 14:23:12.000

Please help.

Thanks.


Try using the EncodeDateTime function declarated in the DateUtils unit.

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;

Important Note from embarcadero site :

To find the fractional number of days between two dates, simply subtract the two values, unless one of the System.TDateTime values is negative. Similarly, to increment a date and time value by a certain fractional number of days, add the fractional number to the date and time value if the System.TDateTime value is positive.

When working with negative System.TDateTime values, computations must handle time portion separately. The fractional part reflects the fraction of a 24-hour day without regard to the sign of the System.TDateTime value. For example, 6:00 am on 12/29/1899 is –1.25, not –1 + 0.25, which would be –0.75. There are no System.TDateTime values between –1 and 0.

for addtional information you can see this link

  • Delphi Date/Time Support


Embarcadero states: There are no System.TDateTime values between –1 and 0.

Even Embarcadero does not seem to know. It is also an error in their helpfile from Delphi 7

It should be, there are no System.TDateTime values between 0 and 1.

dateValue := EncodeDate(1899,12,30);
timeValue := EncodeTime(0,0,1,1);
dateTimeValue := dateValue-timeValue;
dateTimeValue := -1,1585648148e-05
DateTimeToStr(dateTimeValue) -> 30-12-1899 0:00:01

dateValue := EncodeDate(1899,12,30);
timeValue := EncodeTime(23,59,59,999);
dateTimeValue := dateValue-timeValue;
dateTimeValue := -0,99999998843
DateTimeToStr(dateTimeValue) -> 30-12-1899 23:59:59

dateValue := EncodeDate(1899,12,31);
timeValue := EncodeTime(0,0,0,0);
dateTimeValue := dateValue-timeValue;
dateTimeValue := 1
DateTimeToStr(dateTimeValue) -> 31-12-1899

dateValue := EncodeDate(1899,12,31);
timeValue := EncodeTime(0,0,0,0);
dateTimeValue := dateValue+timeValue;
dateTimeValue := 1
DateTimeToStr(dateTimeValue) -> 31-12-1899

dateValue := EncodeDate(1899,12,31);
timeValue := EncodeTime(0,0,0,1);
dateTimeValue := dateValue+timeValue;
dateTimeValue := 1,0000000116
DateTimeToStr(dateTimeValue) -> 31-12-1899

dateValue := EncodeDate(1899,12,31);
timeValue := EncodeTime(23,59,59,999);
dateTimeValue := dateValue+timeValue;
dateTimeValue := 1,99999998843
DateTimeToStr(dateTimeValue) -> 31-12-1899 23:59:59
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜