TimeSpan vs ^TimeSpan

TimeSpan (without caret) is the way to go and makes life easier for comparison operations etc.

TimeSpan is a value type so should always be used without the ^. When you use the caret then you get a boxed value that can't be serialized etc.

General Usage


	//Get time difference
	TimeSpan TimeFromStart;
	TimeFromStart = MyDateTime1 - MyDateTime2;

	//Time something
	DateTime startTime = DateTime.Now;
	Threading::Thread::Sleep(2500);
	DateTime stopTime = DateTime.Now;
	TimeSpan duration = stopTime - startTime;
	TimeSpan twoSecond = new TimeSpan (0, 0, 0, 2, 0);

	if (duration > twoSecond)
		//Above 2 seconds
	else
		//Below 2 seconds

Subtract Time From A DateTime Variable


	TimeSpan Timespan1;
	Timespan1 = TimeSpan(0,0,2);
	StartDateTime = StartDateTime - Timespan1;

Setting A New TimeSpan Value


	Timespan1 = TimeSpan(0,0,1000);	//Set to 1000 seconds

Adding A TimeSpan To A ^DateTime


	DateTime ^EndDateTime;
	TimeSpan Timespan1;

	Timespan1 = TimeSpan(0, cmbImportVideoLength->SelectedIndex, 0);
	EndDateTime = StartDateTime->Add(Timespan1);

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.