1

I am using C++ Builder and am wanting some help to see if two times are the same.

Here is my code:

TDateTime appDateFromVector = TimeOf(appointmentsForFind[i].getAppDateTime());
DateTime appDateFromControl = TimeOf(DateTimePickerAppointmentTime->Time);

These values appear to be the same. I have checked via a ShowMessage statement, and the message displayed is both in time format and they are the same value.

When comparing them though, with the following statement:

if (appDateFromVector == appDateFromControl)

I am not getting a true statement. Is there another process needed to check if two times are the same?

Thanks

4

2 回答 2

1

这是来自 Embarcadero 文档

System::TDateTime 类继承了一个声明为 double 的 val 数据成员,该成员保存了日期时间值。System::TDateTime 值的组成部分是自 1899 年 12 月 30 日以来经过的天数。System::TDateTime 值的小数部分是一天中的时间。

正是这个 double 在使用==运算符时进行了相等性测试,因此非常微小的时间差异可能导致明显相似的时间看起来不相等。您应该考虑测试相等性所需的精度分辨率(例如,精确到秒),然后考虑使用将日期时间转换为适当格式的字符串并测试字符串相等性的函数。

这是我测试时间相等的方式,但我从来不需要比一秒更高的分辨率来进行相等测试。看看这个以将 TDateTime 输出为字符串

于 2012-09-26T12:34:08.627 回答
0

C++Builder 有许多用于比较 TDateTime 值的辅助函数。看看我在下面的示例中包含的 CompareTimeSameTime 。

TDateTime TimeA, TimeB;

// offset TimeB by one hour
TimeA = Now();
TimeB = IncHour(TimeA, 1);

// use CompareTime function
if (CompareTime(TimeA, TimeB) == EqualsValue)
{
  ShowMessage("Both times are equal.");
} 

// use SameTime function
if (SameTime(TimeA, TimeB))
{
  ShowMessage("Both times are equal.");
} 
于 2012-09-27T21:59:43.923 回答