−
It is a rare application that can be developed without in some way needing to work with dates and times. In recongition 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 and extracting elements of dates and times in C#.
+
It is a rare application that can be developed without in some way needing to work with dates and times. In 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 formatting and extracting elements of dates and times in C#.
    
== Creating a C# Date Time Object ==
 
== 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.
 
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 class constructor will set the time to 12:00am. With this in mind, the above code will output the following text:
+
It is important to note that if a time is not specified along with the date, the DateTime class constructor will set the time to 12:00am. With this in mind, the above code will output the following text:
    
<pre>
 
<pre>
 
</table>
 
</table>
   −
An 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:
+
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>
 
</pre>
 
</pre>
   −
The above code, will generate the following output, showing a date 5 days into the future from our original date and time:
+
The above code will generate the following output, showing a date 5 days into the future from our original date and time:
    
<pre>
 
<pre>
 
== Retrieving Parts of a Date and Time ==
 
== Retrieving Parts of a Date and Time ==
   Exception encountered, of type "Error"
[983fedde] /index.php?title=Working_with_Dates_and_Times_in_C_Sharp&diff=7039&oldid=6847 Error from line 434 of /var/www/techotopia/includes/diff/DairikiDiff.php: Call to undefined function each()
Backtrace:
#0 /var/www/techotopia/includes/diff/DairikiDiff.php(544): DiffEngine->diag()
#1 /var/www/techotopia/includes/diff/DairikiDiff.php(344): DiffEngine->compareSeq()
#2 /var/www/techotopia/includes/diff/DairikiDiff.php(227): DiffEngine->diffLocal()
#3 /var/www/techotopia/includes/diff/DairikiDiff.php(721): DiffEngine->diff()
#4 /var/www/techotopia/includes/diff/DairikiDiff.php(859): Diff->__construct()
#5 /var/www/techotopia/includes/diff/DairikiDiff.php(980): MappedDiff->__construct()
#6 /var/www/techotopia/includes/diff/TableDiffFormatter.php(194): WordLevelDiff->__construct()
#7 /var/www/techotopia/includes/diff/DiffFormatter.php(140): TableDiffFormatter->changed()
#8 /var/www/techotopia/includes/diff/DiffFormatter.php(82): DiffFormatter->block()
#9 /var/www/techotopia/includes/diff/DifferenceEngine.php(881): DiffFormatter->format()
#10 /var/www/techotopia/includes/diff/DifferenceEngine.php(797): DifferenceEngine->generateTextDiffBody()
#11 /var/www/techotopia/includes/diff/DifferenceEngine.php(728): DifferenceEngine->generateContentDiffBody()
#12 /var/www/techotopia/includes/diff/DifferenceEngine.php(662): DifferenceEngine->getDiffBody()
#13 /var/www/techotopia/includes/diff/DifferenceEngine.php(632): DifferenceEngine->getDiff()
#14 /var/www/techotopia/includes/diff/DifferenceEngine.php(453): DifferenceEngine->showDiff()
#15 /var/www/techotopia/includes/page/Article.php(797): DifferenceEngine->showDiffPage()
#16 /var/www/techotopia/includes/page/Article.php(508): Article->showDiffPage()
#17 /var/www/techotopia/includes/actions/ViewAction.php(44): Article->view()
#18 /var/www/techotopia/includes/MediaWiki.php(490): ViewAction->show()
#19 /var/www/techotopia/includes/MediaWiki.php(287): MediaWiki->performAction()
#20 /var/www/techotopia/includes/MediaWiki.php(714): MediaWiki->performRequest()
#21 /var/www/techotopia/includes/MediaWiki.php(508): MediaWiki->main()
#22 /var/www/techotopia/index.php(41): MediaWiki->run()
#23 {main}