【问题标题】:C++ calculate difference between dates [duplicate]C ++计算日期之间的差异[重复]
【发布时间】:2012-09-12 14:50:57
【问题描述】:

可能重复:
Checking two TDateTime variables

我无法计算两个日期之间的差异,然后显示差异。我已经发布了以前的问题。

我正在寻求 replaceTime 功能的帮助。我不确定如何/为什么需要使用它。

这是我的代码:

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

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

TDateTime testCombined1 = ReplaceTime(testFirstDate,testFirstTime);
TDateTime testCombined2 = ReplaceTime(testSecondDate,testSecondTime);

TDateTime testDateDifference = testCombined2 - testCombined1;

std::cout << testDateDifference;

【问题讨论】:

  • 您无需多次转发同一个问题:Checking two TDateTime variables
  • 你的问题/疑问是?
  • 在我看来,ReplaceTime 似乎被用来组合日期和时间。您需要结合 testFirstDate 和 testFirstTime 以及 testSecondDate 和 testSecondTime 才能找到差异,不是吗?
  • 是的。基本上,我想知道第二次时间和日期是否晚于第一次时间和日期。

标签: c++ c++builder datediff tdatetime


【解决方案1】:

您的代码没有意义。两个日期之差绝不是新日期!

我怀疑这部分是错误的:

TDateTime testDateDifference = testCombined2 - testCombined1;

两个日期的差异是一个时间段——一个时间跨度(通常以秒为单位)。 TDateTime 的实现是您自己编写的吗?

【讨论】:

  • 是C++ Builder库的TDateTime的实现。他的代码是对的,两个日期之间的差异是另一个日期。
  • Use the System::TDateTime::- to subtract the value specified by the rhs operand from the System::TDateTime::TDateTime object. The rhs operand represents the number of days (including fractional days) to subtract. 根据 Borland 规范,虽然类型是 TDateTime,但 rhs 操作数在技术上不是日期。这就是他们可以摆脱 operator-() 返回新日期的方式。至少 .NET 的 DateTime 类做到了这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2014-04-27
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-02
相关资源
最近更新 更多