Working with Dates and Times in C# - Brief Overview
(Page 2 of 4 )
The DateTime structure has been included ever since the first .NET Framework. It can be found within the System namespaces. It is one of the most generic and straightforward objects to work with. It's not exactly a class, but simply a structure, even though it's a collection of methods, properties, values, and operators. With the help of this structure we can represent an instant in time, whether it is a date, time, or any other related action.
It should be said that this kind of structure can store any date (along with a time) from 00:00:00, January, 1, 0001 A.D. up to 23:59:59, December, 31, 9999 A.D. Without any reasonable doubts, we can claim that this range should suffice.
The real power of the DateTime structure lies in its plethora of built-in methods. Usually we need to work with dates and times, not just store and retrieve them. And libraries were designed for this very purpose-to increase the productivity of developers by providing them with ready-to-use pre-coded functions/methods.
On this page we are going to see how we can create a new DateTime struct, initialize it with some data, and then retrieve it later. Once we have learned how to do these tasks, then we can look into its methods and useful operators.
Before we begin, please remember that the name of the structure is DateTime; as its name suggests, it always holds both date and time values. You cannot store just one or the other. These two go together. All right, now let's call the constructor!
DateTime objDate1 = new DateTime(2008, 9, 28, 2, 20, 35);
DateTime objDate2 = new DateTime.Now;
DateTime objDate3 = new DateTime.Today;
string strDate = "09/28/2008 02:20:35 AM";
DateTime objDate4 = new DateTime.Parse(strDate);
The above examples should make sense right away. Using the properties Today and Now we can get these values from the local computer (depending upon the time zone). You can use the SpecifyKind() method when creating a new DateTime structure, but you want to explicitly specify its kind (such as UTC, local time, or neither). Calling the constructor without any parameters simply creates the first A.D. possible date in range.
The constructor can be overloaded with as many details as necessary (or less than that; just the year is also possible). You can set up a date using all of its parameters such as year, month, day, hours, minutes, and seconds. Then again, what if you want to convert a string into a DateTime structure? If this is what you want, then the Parse() method is what you're going to need. You may want to look into the ParseExact() method, too.
Furthermore, both Parse() and ParseExact() will throw an exception if the conversion fails (i.e.; the supplied string doesn't complies with the required format). If you want a work-around for this, then the TryParse() and TryParseExact() methods might be better options for you. These return a Boolean whether the conversion succeeds or not without throwing an exception.
Should you supply only three parameters to the constructor, without specifying the exact time, it defaults to a value of midnight. Furthermore, there's another way to load the constructor, and that's using 64-bit integer ticks as the only parameter. This value is counted since January 1st of the year 1 A.D, and it measures the total count of 100-nanoseconds ticks since the beginning of the Gregorian calendar.
There are two interesting static properties: MinValue and MaxValue. These two always return the minimum and maximum possible DateTime value, respectively, from that aforementioned range (year 1 and 9999). They are usually used for comparison tasks (such as assigning any non-null value for a date-time variable).
In the next section we're going to discuss operators such as subtraction, addition, and, of course, lots of other useful methods and properties that we're going to need.
Next: Putting DateTime into Action >>
More C# Articles
More By Barzan "Tony" Antal