相差1天22小时。
TDateTime
在 Delphi 和 C++ Builder 中是双精度型,其中整数部分(小数点左边的部分)存储自 1899 年 12 月 30 日基准日期以来的天数(见下面的注释),小数部分(小数点右边的部分)是时间。
减法后您看到的 1899 是因为您的时间少于一整天,因此数字的整个部分为零,正如我提到的零日期是 1899 年 12 月的基准日期。比基准日期晚 1 天(当表示为TDateTime
,该日期被解释为 1899 年 12 月 31 日。
22小时的时间部分大约是0.9167
(实际上,0.916666666666667
),代表一天的 22/24。
Delphi 的运行时库包含一个名为DateUtils
,IIRC 也可用于 C++ Builder(有一个头文件),其中包含可能对您有帮助的函数,例如DaysBetween您可能会发现有用。有可用的 C++ 示例here.
至于平等(一个日期在另一个日期之后),您可以使用标准>
, <
, >=
, <=
, !=
, and ==
运营商。我也在下面演示了这一点。
这是一个简单的示例(在 Delphi 中,因为我在这台机器上没有安装 C++ Builder),可以解释:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, DateUtils;
var
StartDate, EndDate, Diff: TDateTime;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
// Base date, formatted in US date format
WriteLn('BaseDate: ', FormatDateTime('mm/dd/yyyy hh:nn:ss', 0));
StartDate := EncodeDateTime(2012, 9, 11, 14, 0, 0, 0);
EndDate := EncodeDateTime(2012, 9, 12, 16, 0, 0, 0);
Diff := EndDate - StartDate;
WriteLn('Diff as String: ', DateToStr(Diff));
WriteLn('Diff as Double: ', Diff);
WriteLn('DaysBetween: ', DaysBetween(EndDate, StartDate));
// Equality
WriteLn('EndDate after StartDate`, EndDate > StartDate);
RegEx.Free;
ReadLn;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
这会产生以下输出:
BaseDate: 12/30/1899 00:00:00
Diff as String: 12/31/1899
Diff as Double: 1.08333333332848E+0000
DaysBetween: 1
EndDate after StartDate: TRUE
注意:基准日期是由 Microsoft 为 COM 建立的,出于兼容性原因,Delphi/C++ Builder 采用了它。