我知道如何在Delphi中使用codedate将单个YY,MM和DD编码为日期时间字段,或者使用encodetime将单个HH,SS,MM和MS编码为日期时间字段,但有没有办法在日期时间中指定日期和时间领域?
Coz with encodedate我无法指定时间和编码时间我无法指定日期...
例如,如何将日期时间字段设置为2009-11-28 14:23:12.000
请帮忙.
谢谢.
RRU*_*RUZ 31
尝试使用DateUtils单元中声明的EncodeDateTime函数.
function EncodeDateTime(const AYear: Word;
    const AMonth: Word;
    const ADay: Word;
    const AHour: Word;
    const AMinute: Word;
    const ASecond: Word;
    const AMilliSecond: Word): TDateTime;
看这个例子
uses
DateUtils;
var
  myDateTime : TDateTime;
begin
 //Your Code
 myDateTime := EncodeDateTime(2009, 11, 28, 14, 23, 12, 000);
 //Your Code
End;
另外一个选项
uses
SysUtils;
var
myDateTime : TDateTime;
begin
 //Your Code
 myDateTime:= EncodeDate(2009,11,28)+EncodeTime(14,23,12,000);
 //Your Code    
end;
第二个选项有效,因为TDatetime它存储为Double(TDateTime = type Double;),日期作为整数部分(EncodeDate函数返回积分),时间作为小数部分.
TDateTime的日期部分表示自18/30/1899以来经过的天数.一个TDateTime可以是截至9月31日的任何日期(十进制值2,958,465),TDateTime值也可以是负数.十进制值-693593对应于0001年1月1日.
看这些例子
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;
embarcadero网站的重要说明:
要查找两个日期之间的小数天数,只需减去这两个值,除非其中一个System.TDateTime值为负数.同样,要将日期和时间值增加一定的分数天数,请在System.TDateTime值为正数时将分数添加到日期和时间值.
使用负System.TDateTime值时,计算必须单独处理时间部分.小数部分反映了24小时工作日的分数,而不考虑System.TDateTime值的符号.例如,1899年12月29日上午6点是-1.25,而不是-1 + 0.25,这将是-0.75.-1和0之间没有System.TDateTime值.
有关其他信息,您可以看到此链接