delphi - delphi中如何对日期时间进行编码

标签 delphi datetime

我知道如何在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/

相关文章:

delphi - FreeMM 与 ShareMem

ios - 快速日期格式化,从服务器返回的日期与发送到服务器的日期不同

Python:将 Float 格式的时间添加到日期时间值

python - 具有特定时间范围的 Pandas date_range

delphi - 如何从IDE设置项目 `FrameworkType`?

delphi - 将UTF16转换为Windows 1250

delphi - 为什么编译器会说 "statement expected, but expression of type ' <x >' found"?

c# - 时区转换和夏令时

azure - 获取 Microsoft Translator API Azure token 的 400 错误请求和更新

sqlite - ON CONFLICT(id) DO UPDATE SET 不适用于 DELPHI 10.3.1 中的 FireDAC