我知道如何在Delphi中使用encodeate将单个YY、MM和DD编码到日期时间字段中,或使用encodetime将单个HH、SS、MM和MS编码到日期时间字段中,但是有没有办法同时指定日期和时间到日期时间字段?
因为使用encodedate我无法指定时间,而使用encodetime我无法指定日期...
例如如何将日期时间字段设置为 2009-11-28 14:23:12.000
请帮忙。
谢谢。
最佳答案
尝试使用EncodeDateTime在 DateUtils 单元中声明的函数。
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 的日期部分表示自 1899 年 12 月 30 日以来已经过去的天数。 TDateTime 可以是 9999 年 12 月 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 网站的重要说明:
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.
有关其他信息,您可以查看此链接
关于delphi - delphi中如何对日期时间进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1760929/