It is a rare application that can be developed without in some way needing to work with dates and times. In recongition recognition of this fact, the Microsoft engineers responsible for C# gave us the ''DateTime'' object. In this chapter we will look in detail at using this object to work with dates and times in C# based applications. Topics covered include adding and subtracting time, getting the system date and time and formating formatting and extracting elements of dates and times in C#.
== Creating a C# Date Time Object ==
In the above example, after setting the date we use the ''ToString()'' method of the DateTime object to output the current date and time value as a string.
It is important to note that if a time is not specified along with the date, the DateTIme DateTime class constructor will set the time to 12:00am. With this in mind, the above code will output the following text:
<pre>
</table>
An A key issue to understand is that these methods do not change the value of the DateTime object on which the method is called, but rather return a new DateTime object primed with the modified date and time. For example, to add 5 days to our example:
<pre>
</pre>
The above code, will generate the following output, showing a date 5 days into the future from our original date and time: