Member Avatar for Smithy566

I'm trying to compare two DateTime objects, but I am receiving the following error.

error C2664: 'System:: DateTime::Compare' : cannot convert parameter 1 from 'System:: DateTime ^' to 'System:: DateTime'

I'm just trying to use the DateTime 'Compare' method to perform the check. Below is an example to illustrate what I'm trying to do

DateTime^ time1 = gcnew DateTime();
DateTime^ time2 = gcnew DateTime();

time1 = time1->Now;
time2 = time2->Now;

time2->AddHours(5) //add five hours to show difference;

int compareReturnValue = DateTime::Compare(time1 ,time2);

The Compare method should either return: 1,0,-1 depending on the comparision. I don't understand what I'm doing wrong. It would be so much easier if I could just do time1 > time2.

Any help, as always, is very much appreciated.

dynCurrentTime->Now is a handle to a DateTime object? (DateTime ^) or?

If not, I would just declare a regular DateTime object:

DateTime time1,time2;
time1=dynCurrentTime->Now;
etc.

Then you can use the overloaded < > == operators.

Member Avatar for Smithy566

Sorry, I had an error in my example.. dynCurrentTime, dynWaitTime should not have been there. I have replaced them with time1 and time2.

I'm starting to think that its just not possible to use DateTime->Compare with C++ CLI

You need two DateTime objects, not two DateTime handles (using the ^)

DateTime dt1 = DateTime::Now;
DateTime dt2 = DateTime::Now;

int diff = DateTime::Compare(dt1,dt2);

but again, you can use if(dt1 < dt2)

Member Avatar for Smithy566

Thanks jonsca for your fast reply,

That certainly allows me to use the compare method, however, I can no longer call the addHour method. For instance the only options intellisense suggest are to use something like

time1.AddHours(1);

but this won't add 1 hour. Both DateTimes are still the same.

Member Avatar for Smithy566

Its ok, I have worked out that instead of

time1.AddHours(1)

it should have been

time1 = time1.AddHours(1)

Silly Mistake. Thank you for all your help.

Even when I had a DateTime ^ I was not able to find an addHour method (and there was nothing on MSDN about it).

from MSDN

AddHours Returns a new DateTime that adds the specified number of hours to the value of this instance.

You have to make a new DateTime object (or reassign it to itself) to add the hour.

Our posts crossed. No problem :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.