{"id":277,"date":"2017-04-07T09:18:55","date_gmt":"2017-04-07T09:18:55","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=277"},"modified":"2022-06-22T10:05:51","modified_gmt":"2022-06-22T09:05:51","slug":"date-time","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/date-and-time\/date-time","title":{"rendered":"Date &#038; Time"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Loading DateTime With A Time<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tDateTime DateTime1 = new DateTime(\n\t\t\t\t\t\t\t1979,      \/\/ Year\n\t\t\t\t\t\t\t7,        \/\/ Month\n\t\t\t\t\t\t\t28,        \/\/ Day\n\t\t\t\t\t\t\t22,        \/\/ Hour\n\t\t\t\t\t\t\t35,        \/\/ Minute\n\t\t\t\t\t\t\t5,         \/\/ Second\n\t\t\t\t\t\t\t15        \/\/ Millisecond\n\t\t\t\t\t\t\t);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">DateTime now<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tDateTime DateTime1 = DateTime.Now;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Edit DateTime value<\/h4>\n\n\n\n<p>You can&#8217;t edit a DateTime because it&#8217;s immutable. Instead you just create a new one, e.g.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tDateTime DateTime1 = DateTime.Now;\r\n\tDateTime1 = new DateTime(\r\n\t\tDateTime1.Year,\r\n\t\tDateTime1.Month,\r\n\t\tDateTime1.Day,\r\n\t\tDateTime1.Hour,\r\n\t\t0,\t\t\t\/\/DateTime1.Minute,\r\n\t\t0\t\t\t\/\/DateTime1.Second\r\n\t\t);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Add or subtract time<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tDateTime DateTime1 = DateTime.Now;\n\tDateTime1 = DateTime1.AddHours(6);          \/\/Add hours\n\tDateTime1 = DateTime1.AddHours(-26);          \/\/Subtract hours<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting DateTime Value<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> \tDateTime1 = CurrentDateTime;     \/\/Start with DateTime now\n\n\t\/\/Force DateTime1 mins and seconds to 59\n\tDateTime1 = new DateTime(\n\t\t\tDateTime1.Year,\n\t\t\tDateTime1.Month,\n\t\t\tDateTime1.Day,\n\t\t\tDateTime1.Hour,\n\t\t\t59,\n\t\t\t59,\n\t\t\t999);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Display DateTime<\/h4>\n\n\n\n<p><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/8kb3ddd4%28v=vs.110%29.aspx\">Link to all format codes<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyString = String.Format(\"{0:yyyy-MM-dd HH:mm:ss}\", MyDateTime);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Comparing<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\tif (MyDateTime1 &gt;= MyDateTime2)\n\n\t\/\/Compare by date element only (ignore time)\n\tif (MyDateTime1.Date &gt;= MyDateTime2.Date)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Loading DateTime With A Time DateTime now Edit DateTime value You can&#8217;t edit a DateTime because it&#8217;s immutable. Instead you just create a new one, e.g. Add or subtract time Setting DateTime Value Display DateTime Link to all format codes Comparing<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-277","post","type-post","status-publish","format-standard","hentry","category-date-and-time"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=277"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/277\/revisions"}],"predecessor-version":[{"id":592,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/277\/revisions\/592"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}