在c ++ / cli中减去两个DateTime对象

时间:2012-06-09 18:18:03

标签: datetime c++-cli

System::DateTime ^now = System::DateTime::Now;
System::DateTime ^now2 = System::DateTime::Now;
System::TimeSpan ^span = now->Subtract(now2);

上面的代码会出现编译错误:

test.cpp(104) : error C2664: 'System::TimeSpan System::DateTime::Subtract(System::DateTime)' : cannot convert parameter 1 from 'System::DateTime ^' to 'System::DateTime'  
1>        No user-defined-conversion operator available, or  
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called  

now和now2是c ++ / cli句柄,但该方法需要实际的对象。从c ++ / cli调用该方法的正确方法是什么?

2 个答案:

答案 0 :(得分:4)

DateTimeTimeSpan是值类型。

System::DateTime now = System::DateTime::Now; 
System::DateTime now2 = System::DateTime::Now; 
System::TimeSpan span = now.Subtract(now2); 

答案 1 :(得分:0)

System::Diagnostics::Stopwatch^ stopwatch = gcnew System::Diagnostics::Stopwatch();
stopwatch->Start();
// code
stopwatch->Stop();
// stopwatch->Elapsed