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;
	DateTime stopTime = DateTime.Now;
	TimeSpan duration = stopTime - startTime;
	TimeSpan twoSecond = new TimeSpan (0, 0, 0, 2, 0);

	if (duration > twoSecond)
		//Above 2 seconds
		//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);


We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.