{"id":473,"date":"2010-12-21T21:26:36","date_gmt":"2010-12-21T21:26:36","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=473"},"modified":"2022-02-17T06:24:04","modified_gmt":"2022-02-17T06:24:04","slug":"timespan-and-calculating-time-difference","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/date-time\/timespan-and-calculating-time-difference","title":{"rendered":"TimeSpan and Calculating Time Difference"},"content":{"rendered":"<h4>\nTimeSpan vs ^TimeSpan<br \/>\n<\/h4>\n<p>\nTimeSpan (without caret) is the way to go and makes life easier for comparison operations etc.\n<\/p>\n<p>\nTimeSpan is a value type so should always be used&nbsp;<em>without<\/em> the ^. When you use the caret then you get a boxed value that can&#39;t be serialized etc.\n<\/p>\n<h4>\nGeneral Usage<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/Get time difference\r\n\tTimeSpan TimeFromStart;\r\n\tTimeFromStart = MyDateTime1 - MyDateTime2;\r\n\r\n\t\/\/Time something\r\n\tDateTime startTime = DateTime.Now;\r\n\tThreading::Thread::Sleep(2500);\r\n\tDateTime stopTime = DateTime.Now;\r\n\tTimeSpan duration = stopTime - startTime;\r\n\tTimeSpan twoSecond = new TimeSpan (0, 0, 0, 2, 0);\r\n\r\n\tif (duration &gt; twoSecond)\r\n\t\t\/\/Above 2 seconds\r\n\telse\r\n\t\t\/\/Below 2 seconds\r\n<\/code><\/pre>\n<h4>\nSubtract Time From A DateTime Variable<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tTimeSpan Timespan1;\r\n\tTimespan1 = TimeSpan(0,0,2);\r\n\tStartDateTime = StartDateTime - Timespan1;\r\n<\/code><\/pre>\n<h4>\nSetting A New TimeSpan Value<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tTimespan1 = TimeSpan(0,0,1000);\t\/\/Set to 1000 seconds\r\n<\/code><\/pre>\n<h4>\nAdding A TimeSpan To A ^DateTime<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tDateTime ^EndDateTime;\r\n\tTimeSpan Timespan1;\r\n\r\n\tTimespan1 = TimeSpan(0, cmbImportVideoLength-&gt;SelectedIndex, 0);\r\n\tEndDateTime = StartDateTime-&gt;Add(Timespan1);<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&nbsp;without the ^. When you use the caret then you get a boxed value that can&#39;t be serialized etc. General Usage \/\/Get time difference TimeSpan TimeFromStart; TimeFromStart [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-473","post","type-post","status-publish","format-standard","hentry","category-date-time"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=473"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/473\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/473\/revisions\/954"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}