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.

Edited by Smithy566: n/a

5 Years
Discussion Span
Last Post by jonsca

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

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

DateTime time1,time2;

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


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

Edited by Smithy566: n/a


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)


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


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


Its ok, I have worked out that instead of


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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.