{"id":93,"date":"2018-04-04T23:22:00","date_gmt":"2018-04-04T21:22:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/04\/04\/day-6-handling-date-and-time-data-types-in-c\/"},"modified":"2020-08-22T08:56:22","modified_gmt":"2020-08-22T06:56:22","slug":"day-6-handling-date-and-time-data-types-in-c","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/day-6-handling-date-and-time-data-types-in-c\/","title":{"rendered":"Day 6: Handling Date and Time Data Types in C#"},"content":{"rendered":"<p>Hello, good to know that you are making fast progress.<br \/>\nWelcome to day 6 of ASP.NET in 24 days!<\/p>\n<p>Today, we are going the learn how to handle date and time data types in C# as well as conversions.<\/p>\n<p>&nbsp;<\/p>\n<p><b>We would cover the following<\/b><\/p>\n<ol>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/day-6-handling-date-and-time-data-types-in-c#t1\">Using DateTime type<\/a><\/li>\n<li><a href=\"#t2\">Creating an new date using the DateTime constructor<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/day-6-handling-date-and-time-data-types-in-c#t3\">Declaring date data type<\/a><\/li>\n<li><a href=\"#t4\">Converting to a date using Convert.toDateTime()\u00a0<\/a><\/li>\n<li><a href=\"#t5\">Converting Date to a String <\/a><\/li>\n<li><a href=\"#t6\">Getting Today\u2019s date and time\u00a0<\/a><\/li>\n<li><a href=\"#t7\">Extracting Day, Month and Year from Date<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/day-6-handling-date-and-time-data-types-in-c#t8\">About TimeSpan Data Type\u00a0<\/a><\/li>\n<li><a href=\"#t9\">Final Notes<\/a><\/li>\n<\/ol>\n<p><span style=\"color: red;\">Note<\/span>: To run the programs in this lesson, you need to copy the code and use it to replace the main function.<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"t1\"><b>1. Using DateTime type<\/b><\/h3>\n<p>In C# a DateTime data type is a struct type that represents an instant of time.<br \/>\nThe DateTime is a value type that represents dates and times values that range from:<br \/>\n00:00:00, January 1, 0001 to<br \/>\n11:59:59, December 31, 9999<\/p>\n<p>A particular data is the number of ticks since 00:00:00 January 1, 0001. This are just some basic theory you need to just have in mind<\/p>\n<p>&nbsp;<\/p>\n<h3><b id=\"t2\">2. Creating an new date using the DateTime constructor<\/b><\/h3>\n<ul>\n<li>You can create a new datetime object in any of the following ways:<\/li>\n<li>Calling the DateTime constructor and specifying the arguments<\/li>\n<li>Converting a string representation to a datetime using inbuilt functions<\/li>\n<\/ul>\n<p>You can test these two methods using the code below.Create a console application, then copy and run the code snippet below.<\/p>\n<p>&nbsp;<\/p>\n<h3><b id=\"t3\">3. Declaring date data type<\/b><\/h3>\n<p>To declare a DateTime object, you simply using the sytax<\/p>\n<p>DateTime mydate = new DateTime()<\/p>\n<p>In this case, the variable mydate is declared as a DateTime object but has not been assigned a value yet.<\/p>\n<p>&nbsp;<\/p>\n<h3><b id=\"t4\">4. Converting to a date using Convert.toDateTime()<\/b><\/h3>\n<p>One of the ways to convert a value to a DateTime value is using the Convert.ToDateTime() function. I used this method most of the time.<br \/>\nYou can basically convert any other type to DateTime() type. Run the following code snippet to see how it works.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Main<\/span>(<span style=\"color: #333399; font-weight: bold;\">string<\/span>[] args)\r\n{\r\n   Program pr = <span style=\"color: #008800; font-weight: bold;\">new<\/span> Program();\r\n   pr.UsingConvert();\r\n}\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">UsingConvert<\/span>()\r\n{\r\nDateTime mydate = Convert.ToDateTime(<span style=\"color: #6600ee; font-weight: bold;\">1000<\/span>);\r\nConsole.WriteLine();\r\n\r\nConsole.Read();\r\n}\r\n<\/pre>\n<p><b>Listing 1.0:<\/b> Using the Convert.ToDate() function (doesn&#8217;t work!)<\/p>\n<p>&nbsp;<\/p>\n<p>It doesn&#8217;t work! So don&#8217;t use it<br \/>\nIt is however included in the C# documentation;<br \/>\n<ins style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"4209786523\"><\/ins><br \/>\n<b><\/b><\/p>\n<h3><b id=\"t5\">5. Converting Date to a String<\/b><\/h3>\n<p>You can convert a string expression to a DateTime type using any of the following:<br \/>\n<em>Parse()<\/em>: Converts a string to its equivalent DateTime value<br \/>\n<em>ParseExact()<\/em>: This function converts a string to DateTime, but the string needs to match the exact date format, or else, there would be an error.<br \/>\n<em>TryParse():<\/em> Converts string to DateTime but returns a value to indicate if the conversion succeeded or not.<br \/>\n<em>TryParseExact:<\/em> Converts a string to DateTime. The string format much match the date format and also value is returned to indicate success or failure<\/p>\n<p>Run The code snippet below and try to understand how it works<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Main<\/span>(<span style=\"color: #333399; font-weight: bold;\">string<\/span>[] args)\r\n{\r\n   Program pr = <span style=\"color: #008800; font-weight: bold;\">new<\/span> Program();\r\n   pr.UsingParse();\r\n}\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">UsingParse<\/span>()\r\n{\r\n<span style=\"color: #333399; font-weight: bold;\">\u00a0 \u00a0 \u00a0string<\/span> dateInput = <span style=\"background-color: #fff0f0;\">\"Jan 1, 2018\"<\/span>;\r\nDateTime myDate = DateTime.Parse(dateInput);\r\nConsole.WriteLine(myDate);\r\n<span style=\"color: #888888;\">\u00a0 \u00a0 \u00a0\/\/ DISPLAYS THE DATE IN THE FORMATE 1\/1\/2018 12:00:00 AM<\/span>\r\n}\r\n<\/pre>\n<p><b>Listing 2.0<\/b>: Using the Parse Function<\/p>\n<p>&nbsp;<\/p>\n<p>The listing 2.0 shows the conversion to DateTime() using Parse().<br \/>\n<i><b>Quiz<\/b><\/i>: Try to modify the code to use other Parse functions you have learnt here.<\/p>\n<p>&nbsp;<\/p>\n<h3><b>6. Getting Today\u2019s date and time<\/b><\/h3>\n<p>There are many ways you can get the current date and time using the C# code:<\/p>\n<ul>\n<li>DateTime.Today: Returns the current date<\/li>\n<li>DateTime.Utc.Now.Date: Returns the current date<\/li>\n<li>DateTime.Now.ToString(&#8220;HH:mm:ss&#8221;);<\/li>\n<li>DateTime.Today.ToString(&#8220;dd-MM-yyyy&#8221;);<\/li>\n<\/ul>\n<p>Try out all this methods by running the code snippet below.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Main<\/span>(<span style=\"color: #333399; font-weight: bold;\">string<\/span>[] args)\r\n{\r\n   Program pr = <span style=\"color: #008800; font-weight: bold;\">new<\/span> Program();\r\n   pr.WorkingWithDate();\r\n}\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">WorkingWithDate<\/span>()\r\n{\r\nConsole.WriteLine(DateTime.Today); <span style=\"color: #888888;\">\/\/Returns the current date<\/span>\r\nConsole.WriteLine(DateTime.UtcNow.Date); <span style=\"color: #888888;\">\/\/Returns the current date<\/span>\r\nConsole.WriteLine(DateTime.Now.ToString(<span style=\"background-color: #fff0f0;\">\"HH:mm:ss\"<\/span>));\r\nConsole.WriteLine(DateTime.Today.ToString(<span style=\"background-color: #fff0f0;\">\"dd-MM-yyyy\"<\/span>));\r\nDateTime.Now.ToString(<span style=\"background-color: #fff0f0;\">\"yyyy-MM-dd hh:mm:ss\"<\/span>);\r\n}\r\n<\/pre>\n<p><b>Listing 3.0<\/b>: Getting Current Date and Time<\/p>\n<p>&nbsp;<\/p>\n<h3><b>7. Extracting Day, Month and Year from Date<\/b><\/h3>\n<p>You can extract the day, month or year part of a given date. When they are extracted, you can use then either as string or any other datatype. Here we assume we extract them as string.<br \/>\nSo, the Listing 4.0, gives the code snippet that does the splitting. It is very clear and easy to understand. Run it and make sure you understands how it works.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Main<\/span>(<span style=\"color: #333399; font-weight: bold;\">string<\/span>[] args)\r\n{\r\n   Program pr = <span style=\"color: #008800; font-weight: bold;\">new<\/span> Program();\r\n   pr.SplitingDates();\r\n}\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">SplitingDates<\/span>()\r\n{\r\nString myDate = DateTime.Now.ToString(); <span style=\"color: #888888;\">\/\/GET THE CURRENT DATE<\/span>\r\nDateTime datevalue = (Convert.ToDateTime(myDate.ToString())); <span style=\"color: #888888;\">\/\/CONVERTS THE myDate to a\u00a0 \u00a0 \u00a0 DateTime TYPE<\/span>\r\n\r\nString dy = datevalue.Day.ToString();\r\nString mn = datevalue.Month.ToString();\r\nString yr = datevalue.Year.ToString();\r\n\r\nConsole.WriteLine(<span style=\"background-color: #fff0f0;\">\"The day is: \"<\/span> + dy);\r\nConsole.WriteLine(<span style=\"background-color: #fff0f0;\">\"The month is: \"<\/span> + mn);\r\nConsole.WriteLine(<span style=\"background-color: #fff0f0;\">\"The year is: \"<\/span> + yr);\r\n}\r\n<\/pre>\n<p>Listing 4.0: Extracting Day, Month and Year from a date<\/p>\n<p>&nbsp;<\/p>\n<h3><b>8. About TimeSpan Data Type<\/b><\/h3>\n<p>According the the msdn documentation, a TimeSpan represents a time interval.<br \/>\nThink of TimeSpan as a diffrence in time. When you subtract or add two dates, the result you have will be a TimeSpan. That is why it is called &#8216;a time interval&#8217;.<\/p>\n<p>Just like a Date contains day, month and year, a TimeSpan contains the following:<\/p>\n<div>\n<ul>\n<li>Days<\/li>\n<li>Hours<\/li>\n<li>Milliseconds<\/li>\n<li>Minutes<\/li>\n<li>Second<\/li>\n<\/ul>\n<\/div>\n<p>The Hours property of TimeSpan represents whole hours, while the TotalHours property represents whole and fractional hours of the TimeSpan.<br \/>\nHow TimeSpan works is illustrated in Listing 5.0. Please try to make sure you copy and run the code:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Main<\/span>(<span style=\"color: #333399; font-weight: bold;\">string<\/span>[] args)\r\n{\r\n   Program pr = <span style=\"color: #008800; font-weight: bold;\">new<\/span> Program();\r\n   pr.UsingTimeSpan();\r\n}\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">UsingTimeSpan<\/span>()\r\n{\r\nTimeSpan span = DateTime.Now - DateTime.Now.Date;\r\n<span style=\"color: #333399; font-weight: bold;\">\u00a0 \u00a0 int<\/span> days = span.Days;\r\n<span style=\"color: #333399; font-weight: bold;\">\u00a0 \u00a0 int<\/span> hours = span.Hours;\r\n<span style=\"color: #333399; font-weight: bold;\">\u00a0 \u00a0 int<\/span> mins = span.Minutes;\r\n<span style=\"color: #333399; font-weight: bold;\">\u00a0 \u00a0 int<\/span> seconds = span.Seconds;\r\n<span style=\"color: #333399; font-weight: bold;\">\u00a0 \u00a0 int<\/span> milisecs = span.Milliseconds;\r\n\r\nConsole.WriteLine(<span style=\"background-color: #fff0f0;\">\"Days: \"<\/span> + days);\r\nConsole.WriteLine(<span style=\"background-color: #fff0f0;\">\"Hours: \"<\/span> + hours);\r\nConsole.WriteLine(<span style=\"background-color: #fff0f0;\">\"Minutes: \"<\/span> + mins);\r\nConsole.WriteLine(<span style=\"background-color: #fff0f0;\">\"Seconds: \"<\/span> + seconds);\r\nConsole.WriteLine(<span style=\"background-color: #fff0f0;\">\"Milliseconds: \"<\/span> + milisecs);\r\n}\r\n<\/pre>\n<p><b>Listing 5.0<\/b>: Using TimeSpan in C#<\/p>\n<p>&nbsp;<\/p>\n<h3><b>9. Final Note<\/b><\/h3>\n<p>Congrats if you have come this far!<br \/>\nIf you encountered any challenges implementing it in the comment box below and someone would give you the needed assistance.<\/p>\n<p><ins style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"4209786523\"><\/ins><br \/>\n<ins style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-slot=\"4209786523\"><\/ins><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, good to know that you are making fast progress. Welcome to day 6 of ASP.NET in 24 days! Today, we are going the learn &hellip; <\/p>\n","protected":false},"author":2,"featured_media":670,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[363],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/93"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":11,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":1301,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions\/1301"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/670"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}