Changes

Jump to: navigation, search

Working with Dates and Times in C Sharp

2,916 bytes added, 21:06, 19 February 2008
Formating Dates and Times in C#
<pre>
DateTime meetingAppt = new DateTime(2008, 9, 22, 14, 30, 0);
 
System.Console.WriteLine(meetingAppt.ToLongDateString()); // Monday, September 22, 2008
System.Console.WriteLine(meetingAppt.ToShortDateString()); // 9/22/2008
System.Console.WriteLine(meetingAppt.ToShortTimeString()); // 2:30 PM
</pre>
 
If the above prepackaged formatting methods do not provide the required output a formidable variety of custom formats may be constructing using the ''ToString()'' method. The ''ToString()'' method takes as an argument a ''format'' string which specifies precisely how the date and time is to be displayed. The following example show a few of this formats in action. The subsequent table lists all the possible format variables which may be used:
 
<pre>
</pre>
 
 
== Adding or Subtracting from Dates and Times ==
 
The C# DateTime object provides a number of methods for adding or subtracting date and times from a DateTime object instance. These methods are outlined the following table:
 
<table border="1" cellspacing="0">
<tr style="background:#efefef;">
<th>Method<th>Description</th>
</tr>
<tr>
<td>Add<td>Adds/Subtracts the value of the specified TimeSpan object instance.</td>
<tr>
<td>AddDays<td>Adds/Subtracts the specified number of days</td>
<tr>
<td>AddHours<td>Adds/Subtracts the specified number of hours</td>
<tr>
<td>AddMilliseconds<td>Adds/Subtracts the specified number of Milliseconds</td>
<tr>
<td>AddMinutes<td>Adds/Subtracts the specified number of minutes</td>
<tr>
<td>AddMonths<td>Adds/Subtracts the specified number of months</td>
<tr>
<td>AddSeconds<td>Adds/Subtracts the specified number of seconds</td>
<tr>
<td>AddYears<td>Adds/Subtracts the specified number of years</td>
</tr>
</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:
 
<pre>
using System;
 
class TimeDemo
{
static void Main()
{
DateTime meetingAppt = new DateTime(2008, 9, 22, 14, 30, 0);
DateTime newAppt = meetingAppt.AddDays(5);
System.Console.WriteLine (newAppt.ToString());
}
}
</pre>
 
The above code, will generate the following output, showing a date 5 days into the future from our original date and time:
 
<pre>
9/27/2008 14:30:00 PM
</pre>
 
To subtract from a date and time simply pass through a negative value to the appropriate method. For example, to subtract 10 months from our example object:
 
<pre>
using System;
 
class TimeDemo
{
static void Main()
{
DateTime meetingAppt = new DateTime(2008, 9, 22, 14, 30, 0);
DateTime newAppt = meetingAppt.AddMonths(-10);
System.Console.WriteLine (newAppt.ToString());
}
}
</pre>
 
Resulting in the following output:
 
<pre>
11/22/2007 2:30:00 PM
</pre>
== Adding or Subtracting from Dates and Times ==

Navigation menu