Wednesday, May 21, 2014

Getting dates and calculating a time interval in C# - Part I

I wanted to create a console app in Visual C# which required the app to choose a path based on the number of days elapsed between the current date and a date entered by the user. I assumed I would do a quick search and find a way to do this on MSDN or Stack Overflow. I assumed many people had done this many times and code snippets would be posted all over the web. Wrong. Sure, I found a way to subtract one date from another if I entered the dates as arguments or the app was a form. But I couldn't find a way to do it when 1) the app was a console app, 2) the app provided the current date and 2) the user entered another date which would be subtracted from the current date.

This is a two-part post explaining the program I wrote to resolve the problem. Part I explains how to get the dates. The second part will explain how to subtract the dates to get the time interval. The full source code is on GitHub.

Today is the day
Getting today's date is easy:

     Console.WriteLine("Today's date is {0}.", DateTime.Today);

Microsoft has already provided us with the DateTime struct and its property, Today. By reviewing the source code, you can see that Today is a static property.  Static properties can be called without an object being created, hence, the reason the code snippet above will display the current date and time.

However, for my purposes, there are a couple of problems with this code. First, this format will give you a time of 12:00:00 AM as well as the date. For my app, I didn't need the time. Second, I didn't need to merely display the current date, I needed to store it so that another date could be subtracted from it. As painfully obvious as this is now, I wasn't sure how to do that. If you aren't either, read on.

Construct Today
Starting with the basics, DateTime is structure (a.k.a. struct) in the System namespace. You do not need to use keyword "new" to instantiate a struct object. Create the object like so to display only the date:

     DateTime dateOnly = DateTime.Today;
     Console.WriteLine("Today's date is {0}.", dateOnly.ToString("d");

     (The "d" is a short date format specifier. In other words, this format will give you the date and not the time.)

You may also want to read DotNetPerls which explains why you would choose the Today property over the Now property.

User Input
Since DateTime is a struct, that means it is a type. After I ask the user to input a date, I declare a DateTime variable and convert the line entered by the user to DateTime type. Just as with the code snippet above, though, this will display the date and a time. So the next step is to create a DateTime object and use the short date format specifier in the Console.WriteLine argument.

         Console.WriteLine("Enter the date you bought your vehicle.");
         Console.WriteLine("Use this format, 01/01/2011.");
         DateTime whenBought = Convert.ToDateTime(Console.ReadLine());
         DateTime boughtDateOnly = whenBought.Date;
         Console.WriteLine("Vehicle was bought on {0}.", boughtDateOnly.ToString("d"));

Here is a screen shot of the output.


The next post will go over TimeSpan and its Subtract method.

No comments:

Post a Comment