检查两个TDateTime变量 [英] Checking two TDateTime variables

查看:246
本文介绍了检查两个TDateTime变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++ Builder并有以下问题:

I am using C++ Builder and have the following question:

我想检测日期/时间是否晚于另一个日期/时间,

I am wanting to detect if a date/time is later than another date/time, and by how much.

这是我当前的代码:

TDateTime testFirstDate("11/09/2012");
TDateTime testFirstTime("14:00");

TDateTime testSecondDate("12/09/2012");
TDateTime testSecondTime("16:00");

TDateTime testCombined1 = testFirstDate + testFirstTime;
TDateTime testCombined2 = testSecondDate + testSecondTime;

TDateTime testDateDifference = testSecondDate - testFirstDate;
std::cout << testDateDifference;

在上述示例中,将打印以下内容:31/12/1899

In the above example, the following gets printed out: 31/12/1899

两个值之间的差异只有1天。为什么是:31/12/1899被打印,而不是像1:

The difference between the two values is only 1 day. Why is: 31/12/1899 being printed, and not something like: 1?

推荐答案

区别是1天,22小时。

The difference is 1 day, 22 hours.

在Delphi和C ++ Builder中的TDateTime 是一个双精度,其中整个部分的小数点)存储自1899年12月30日的基准日起的天数(见下面的注释),小数部分(小数点右边的部分)是时间。

TDateTime in Delphi and C++ Builder is a double, where the whole portion (the part to the left of the decimal point) stores the number of days since a base date of December 30, 1899 (see note below), and the fractional portion (the part to the right of the decimal point) is the time.

减法后看到的1899是因为你不到一整天,因此数字的整个部分都是零,正如我提到的日期零是1899年12月的基准日期。由于您的日期晚于基准日期1天(表示为 TDateTime 时,日期将解释为12月31日,1899。

The 1899 you're seeing after the subtraction is because you have less than a full day, and therefore the whole portion of the number is zero, and as I mentioned a date of zero is the base date in December, 1899. Since your date is 1 day later than that base date (when represented as a TDateTime, the date is interpreted as December 31, 1899.

22小时的时间约为 0.9167 (实际上, 0.916666666666667

The time portion for 22 hours is approximately 0.9167 (actually, 0.916666666666667), which represents 22/24ths of a day.

Delphi的运行时库包含一个名为 DateUtils ,其中IIRC也可用于C ++ Builder(有一个头文件),其中包含可能会帮助你的功能,例如 DaysBetween 你可能会发现有用的C ++的例子可以使用此处

Delphi's runtime library contains a unit called DateUtils, which IIRC is available to C++ Builder as well (there's a header file for it), which contains functions which may help you, like DaysBetween that you may find useful. There are C++ examples of it's use available here.

至于(一个日期是另一个日期),您可以使用标准> < > = < = != c $ c> == 运算符。

As far as equality (one date being after the other), you can use the standard >, <, >=, <=, !=, and == operators. I've demonstrated this below as well.

下面是一个简单的例子(在Delphi中,我没有在这台机器上安装C ++ Builder),可能会解释: / p>

Here's a quick example (in Delp as I don't have C++ Builder installed on this machine) that might explain:

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采用了它。

NOTE: The base date was established by Microsoft for COM, and for compatibility reasons Delphi/C++ Builder adopted it.

这篇关于检查两个TDateTime变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆