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

stukelly
  • 4,257
  • 3
  • 37
  • 44
user1690531
  • 231
  • 1
  • 6
  • 17

2 Answers2

1

This is from the Embarcadero documentation

The System::TDateTime class inherits a val data member declared as a double that holds the date-time value. The integral part of a System::TDateTime value is the number of days that have passed since 12/30/1899. The fractional part of a System::TDateTime value is the time of day.

It is this double that is tested for equality when using the == operator and so very tiny differences in time can result in apparently similar times appearing unequal. You should think about the accuracy resolution that you require to test for equality (for example, to the nearest second) and then consider using the functions that convert a date time to an appropriately formatted string and test for equality of the strings.

This is the way I test for equality of times, but I never need a higher resolution than one second for an equality test. Take a look at this for outputting a TDateTime as a string

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
0

C++Builder has a number of helper functions for comparing TDateTime values. Have a look at CompareTime and SameTime, which I have included in the example below.

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.");
} 
BenMorel
  • 34,448
  • 50
  • 182
  • 322
stukelly
  • 4,257
  • 3
  • 37
  • 44