{"id":471,"date":"2010-12-21T21:25:00","date_gmt":"2010-12-21T21:25:00","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=471"},"modified":"2022-02-17T06:24:04","modified_gmt":"2022-02-17T06:24:04","slug":"date-time","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/date-time\/date-time","title":{"rendered":"Date &#038; Time"},"content":{"rendered":"<h4>\nDateTime vs ^DateTime<br \/>\n<\/h4>\n<p>\nDateTime (without caret) is the way to go and makes life easier for comparison operations etc.\n<\/p>\n<p>\nDateTime is a value type so should always be used <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>\nUsing DateTime<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tDateTime DateTimeNow;\r\n\tDateTimeNow = DateTime::Now;\r\n\r\n\t\/\/or\r\n\tDateTimeNow = DateTime(0);\r\n\r\n\tMyString = DateTimeNow.Year + DateTimeNow.Month + DateTimeNow.Day;\r\n\r\n\t\/\/or\r\n\tMyString = String::Format(&quot;{0:D2}&quot;, DateTimeNow.Year);\r\n\r\n\t\/\/or\r\n\tMyString = String::Format(&quot;{0:G}&quot;, DateTimeNow);\r\n\r\n\tarray ^MyDateTimes;\r\n<\/code><\/pre>\n<h4>\nUsing ^DateTime<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tDateTime ^DateTimeNow = gcnew DateTime;\r\n\tDateTimeNow = DateTime::Now;\r\n\r\n\tMyString = DateTimeNow-&gt;Year + DateTimeNow-&gt;Month + DateTimeNow-&gt;Day;\r\n\r\n\t\/\/or\r\n\tMyString = String::Format(&quot;{0:D2}&quot;, DateTimeNow-&gt;Year);\r\n\r\n\t\/\/or\r\n\tMyString = String::Format(&quot;{0:G}&quot;, DateTimeNow);\r\n\r\n\t\/\/When you want to convert to DateTime to pass a value or to use DateTime methods\r\n\tConvert::ToDateTime(MyDateTime)\r\n<\/code><\/pre>\n<h4>\nComparing<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tif (MyDateTime1 &gt;= MyDateTime2)\r\n\r\n\t\/\/Compare by date element only (ignore time)\r\n\tif (MyDateTime1.Date &gt;= MyDateTime2.Date)\r\n<\/code><\/pre>\n<h4>\nAdjusting<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tMyDateTime2 = MyDateTime1 + TimeSpan(0, 0, 1)\r\n<\/code><\/pre>\n<h4>\nAdding the current time and date to a string<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\ttxtHistory-&gt;Text = &quot;(&quot; + DateTime::Now + &quot;)&quot;;\r\n<\/code><\/pre>\n<h4>\nAdding Values From Strings<br \/>\n<\/h4>\n<p>\nThe date and time fields are all int values but they are read only. I think you can only write an entire date time object to a DateTime object, like this:\n<\/p>\n<pre>\r\n<code>\r\n\tSystem::DateTime moment = System::DateTime(1999, 1, 13, 3, 57, 32, 11);\r\n\t\/\/Year, Month, Day, Hour, Minute, Second, Millisecond (overloaded - don&#39;t have to include all of these)\r\n\r\n\tSystem::DateTime dateTime =\r\n\t   System::DateTime( 1979,      \/\/ Year\r\n                     7,        \/\/ Month\r\n                     28,        \/\/ Day\r\n                     22,        \/\/ Hour\r\n                     35,        \/\/ Minute\r\n                     5,         \/\/ Second\r\n                     15        \/\/ Millisecond\r\n                     );\r\n<\/code><\/pre>\n<h4>\nConvert To Universal Time Format String<br \/>\n<\/h4>\n<p>\nConverting a DateTime to this format allows you to avoid local date time format issues when passing DateTime values between systems. Then just use Convert::ToDateTime to convert it back.\n<\/p>\n<pre>\r\n<code>\r\n\tString ^sTemp = MyDateTimeValue-&gt;ToString(&quot;yyyy-MM-ddTHH:mm:ss.fffffff&quot;);\r\n<\/code><\/pre>\n<h4>\nUTC<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tDateTime ^TimeNow = DateTime();\r\n\tTimeNow = DateTime::Now;\r\n\tTimeNow = TimeNow-&gt;ToUniversalTime();\r\n<\/code><\/pre>\n<h4>\nValues<br \/>\n<\/h4>\n<pre>\r\n<code>\r\nDayOfWeek\t\tDayOfWeek::Thursday\r\n<\/code><\/pre>\n<h4>\nProblems With Array&lt;DateTime^&gt; Object not being accepted<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\/\/Using this to force the DateTime to be converted from ^ to a standard DateTime:\r\n\tConvert::ToDateTime(\r\n\/\/is the trick to use when you get the compiler refusign to accept a perfectly valid line!\r\n<\/code><\/pre>\n<h4>\nFormat<br \/>\n<\/h4>\n<p>\n<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/8kb3ddd4%28v=vs.110%29.aspx\">Link to all format codes<\/a>\n<\/p>\n<pre>\r\n<code>\r\n\tMyString = String::Format(&quot;{0:dd\/MM\/yyyy HH:mm:ss}&quot;, MyDateTime);\r\n\r\n\r\n\tDateTime ^thisDate = DateTime::Now;\r\n\t\/\/ Store the output of the String::Format method in a string.\r\n\tString ^resultString = &quot;&quot;;\r\n\tConsole::Clear();\r\n\r\n    \/\/ Format a negative integer or floating-point number in\r\n    \/\/ various ways.\r\n    Console::WriteLine(&quot;Standard Numeric Format Specifiers&quot;);\r\n    resultString = String::Format(CultureInfo::CurrentCulture,\r\n        &quot;(C) Currency: . . . . . . . . {0:C}\\n&quot; +\r\n        &quot;(D) Decimal:. . . . . . . . . {0:D}\\n&quot; +\r\n        &quot;(E) Scientific: . . . . . . . {1:E}\\n&quot; +\r\n        &quot;(F) Fixed point:. . . . . . . {1:F}\\n&quot; +\r\n        &quot;(G) General:. . . . . . . . . {0:G}\\n&quot; +\r\n        &quot;    (default):. . . . . . . . {0} (default = &#39;G&#39;)\\n&quot; +\r\n        &quot;(N) Number: . . . . . . . . . {0:N}\\n&quot; +\r\n        &quot;(P) Percent:. . . . . . . . . {1:P}\\n&quot; +\r\n        &quot;(R) Round-trip: . . . . . . . {1:R}\\n&quot; +\r\n        &quot;(X) Hexadecimal:. . . . . . . {0:X}\\n&quot;,\r\n        -123, -123.45f);\r\n    Console::WriteLine(resultString);\r\n\r\n    \/\/ Format the current date in various ways.\r\n    Console::WriteLine(&quot;Standard DateTime Format Specifiers&quot;);\r\n    resultString = String::Format(CultureInfo::CurrentCulture,\r\n        &quot;(d) Short date: . . . . . . . {0:d}\\n&quot; +\r\n        &quot;(D) Long date:. . . . . . . . {0:D}\\n&quot; +\r\n        &quot;(t) Short time: . . . . . . . {0:t}\\n&quot; +\r\n        &quot;(T) Long time:. . . . . . . . {0:T}\\n&quot; +\r\n        &quot;(f) Full date\/short time: . . {0:f}\\n&quot; +\r\n        &quot;(F) Full date\/long time:. . . {0:F}\\n&quot; +\r\n        &quot;(g) General date\/short time:. {0:g}\\n&quot; +\r\n        &quot;(G) General date\/long time: . {0:G}\\n&quot; +\r\n        &quot;    (default):. . . . . . . . {0} (default = &#39;G&#39;)\\n&quot; +\r\n        &quot;(M) Month:. . . . . . . . . . {0:M}\\n&quot; +\r\n        &quot;(R) RFC1123:. . . . . . . . . {0:R}\\n&quot; +\r\n        &quot;(s) Sortable: . . . . . . . . {0:s}\\n&quot; +\r\n        &quot;(u) Universal sortable: . . . {0:u} (invariant)\\n&quot; +\r\n        &quot;(U) Universal full date\/time: {0:U}\\n&quot; +\r\n        &quot;(Y) Year: . . . . . . . . . . {0:Y}\\n&quot;,\r\n        thisDate);\r\n    Console::WriteLine(resultString);\r\n\r\n    \/\/ Format a Color enumeration value in various ways.\r\n    Console::WriteLine(&quot;Standard Enumeration Format Specifiers&quot;);\r\n    resultString = String::Format(CultureInfo::CurrentCulture,\r\n        &quot;(G) General:. . . . . . . . . {0:G}\\n&quot; +\r\n        &quot;    (default):. . . . . . . . {0} (default = &#39;G&#39;)\\n&quot; +\r\n        &quot;(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\\n&quot; +\r\n        &quot;(D) Decimal number: . . . . . {0:D}\\n&quot; +\r\n        &quot;(X) Hexadecimal:. . . . . . . {0:X}\\n&quot;,\r\n        Color::Green);\r\n    Console::WriteLine(resultString);\r\n};\r\n\/*\r\nThis code example produces the following results:\r\n\r\nStandard Numeric Format Specifiers\r\n(C) Currency: . . . . . . . . ($123.00)\r\n(D) Decimal:. . . . . . . . . -123\r\n(E) Scientific: . . . . . . . -1.234500E+002\r\n(F) Fixed point:. . . . . . . -123.45\r\n(G) General:. . . . . . . . . -123\r\n(default):. . . . . . . . -123 (default = &#39;G&#39;)\r\n(N) Number: . . . . . . . . . -123.00\r\n(P) Percent:. . . . . . . . . -12,345.00 %\r\n(R) Round-trip: . . . . . . . -123.45\r\n(X) Hexadecimal:. . . . . . . FFFFFF85\r\n\r\nStandard DateTime Format Specifiers\r\n(d) Short date: . . . . . . . 6\/26\/2004\r\n(D) Long date:. . . . . . . . Saturday, June 26, 2004\r\n(t) Short time: . . . . . . . 8:11 PM\r\n(T) Long time:. . . . . . . . 8:11:04 PM\r\n(f) Full date\/short time: . . Saturday, June 26, 2004 8:11 PM\r\n(F) Full date\/long time:. . . Saturday, June 26, 2004 8:11:04 PM\r\n(g) General date\/short time:. 6\/26\/2004 8:11 PM\r\n(G) General date\/long time: . 6\/26\/2004 8:11:04 PM\r\n(default):. . . . . . . . 6\/26\/2004 8:11:04 PM (default = &#39;G&#39;)\r\n(M) Month:. . . . . . . . . . June 26\r\n(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT\r\n(s) Sortable: . . . . . . . . 2004-06-26T20:11:04\r\n(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)\r\n(U) Universal full date\/time: Sunday, June 27, 2004 3:11:04 AM\r\n(Y) Year: . . . . . . . . . . June, 2004\r\n\r\nStandard Enumeration Format Specifiers\r\n(G) General:. . . . . . . . . Green\r\n(default):. . . . . . . . Green (default = &#39;G&#39;)\r\n(F) Flags:. . . . . . . . . . Green (flags or integer)\r\n(D) Decimal number: . . . . . 3\r\n(X) Hexadecimal:. . . . . . . 00000003\r\n\r\n*\/\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>DateTime vs ^DateTime DateTime (without caret) is the way to go and makes life easier for comparison operations etc. DateTime is a value type so should always be used without the ^. When you use the caret then you get a boxed value that can&#39;t be serialized etc. Using DateTime DateTime DateTimeNow; DateTimeNow = DateTime::Now; [&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-471","post","type-post","status-publish","format-standard","hentry","category-date-time"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/471","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=471"}],"version-history":[{"count":13,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/471\/revisions"}],"predecessor-version":[{"id":1369,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/471\/revisions\/1369"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}