{"id":116,"date":"2021-06-15T16:53:47","date_gmt":"2021-06-15T16:53:47","guid":{"rendered":"https:\/\/shiblenoman.com\/blog\/?p=116"},"modified":"2021-06-15T16:53:47","modified_gmt":"2021-06-15T16:53:47","slug":"extract-year-from-database-date-in-c","status":"publish","type":"post","link":"https:\/\/aabirtech.com\/shiblenomanblog\/2021\/06\/15\/extract-year-from-database-date-in-c\/","title":{"rendered":"Extract year from database date in C#"},"content":{"rendered":"\n<p>In one of my projects, I was needed to extract a year from a database query and use it at a later stage in the program. I had to declare a nullable Datetime variable and assign the value from the database in a loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DateTime? expiryDate = DateTime.Now;\n<\/code><\/pre>\n\n\n\n<p>Database query result need to assign in expiryDate variable <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foreach (dynamic item in data)\r\n            {                \r\n                expiryDate = DateTime.ParseExact(item.ExpiryDate.ToString(),\r\n                                                \"dd\/MM\/yyyy HH:mm:ss\",\r\n                                                CultureInfo.InvariantCulture,\r\n                                                DateTimeStyles.AdjustToUniversal);\r\n            }<\/code><\/pre>\n\n\n\n<p>The date format in the database was like this &#8220;2021-06-30 00:00:00&#8221;, which required to convert to &#8220;dd\/MM\/yyyy&#8221; format. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int year = expiryDate.Value.Year<\/code><\/pre>\n\n\n\n<p>The above line of code will output the year from the expiryDate variable. We can get day and month from this variable in the same way. <\/p>\n\n\n\n<p>Happy Coding \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of my projects, I was needed to extract a year from a database query and use it at a later stage in the program. I had to declare a nullable Datetime variable and assign the value from the database in a loop. Database query result need to assign in expiryDate variable The date [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-asp-net-core"],"acf":[],"_links":{"self":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":4,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/posts\/116\/revisions\/120"}],"wp:attachment":[{"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aabirtech.com\/shiblenomanblog\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}