
.NET 1.1+DateTime Manipulation
The twenty-seventh part of the C# Fundamentals tutorial completes the examination of the DateTime data type provided by C# and the .NET Framework. This article considers manipulation of DateTime data and formatting date and time information as a string.
DateTime Value Manipulation
The previous articles in the C# Fundamentals tutorial have examined the creation and reading of DateTime data and the structure's constituent parts. In this article we will manipulate DateTime information, starting by adjusting DateTime values. This can be achieved using simple operators or, for more detailed control, using DateTime methods.
Addition and Subtraction Operators
Two arithmetic operators can be used with the DateTime structure. These are addition (+) and subtraction (-). Each requires two operands. The first is the DateTime value to modify and the second is a TimeSpan containing the amount of time to add or remove.
DateTime theDate = DateTime.Parse("2 Jan 2007 20:15:00");
// Add one hour, one minute and one second
theDate = theDate + new TimeSpan(1, 1, 1); // theDate = 2 Jan 2007 21:16:01
// Subtract twenty-five days
theDate = theDate - new TimeSpan(25, 0, 0, 0); // theDate = 8 Dec 2006 21:16:01
The two arithmetic operators may also be used as compound assignment operators. The following demonstrates this after using the Parse method to create a TimeSpan value from a string.
DateTime theDate = DateTime.Parse("2 Jan 2007 20:15:00");
// Add one hour, one minute and one second
theDate += TimeSpan.Parse("01:01:01"); // theDate = 2 Jan 2007 21:16:01
// Subtract twenty-five days
theDate -= TimeSpan.Parse("25.00:00:00"); // theDate = 8 Dec 2006 21:16:01
Add Methods
The Add methods allow more control over the processing of DateTime values. The first of which is the Add method itself, which adds a TimeSpan to a DateTime:
DateTime theDate = DateTime.Parse("2 Jan 2007 20:15:00");
TimeSpan duration = TimeSpan.Parse("1.01:01:01");
theDate = theDate.Add(duration); // theDate = 3 Jan 2007 21:16:01
The Add method provides no greater control over the calculation than the simple arithmetic operators. To gain flexibility, we can use methods for adding a number of years, months, etc. There is even an Add method used to add ticks; a single tick being one ten-millionth of a second. These methods are all named Add followed by the unit of time to be added. Values may be subtracted by passing a negative parameter to an Add method. Each method accepts a single double-precision number as a parameter except for AddTicks, which requires a long integer parameter.
DateTime theDate = DateTime.Parse("2 Jan 2007");
theDate = theDate.AddYears(1); // theDate = 2 Jan 2008
theDate = theDate.AddMonths(2); // theDate = 2 Mar 2008
theDate = theDate.AddDays(1.5); // theDate = 3 Mar 2008 12:00:00
theDate = theDate.AddHours(-6); // theDate = 2 Mar 2008 06:00:00
theDate = theDate.AddMinutes(150); // theDate = 2 Mar 2008 08:30:00
theDate = theDate.AddSeconds(10.5); // theDate = 2 Mar 2008 08:30:10.5
theDate = theDate.AddMilliseconds(499); // theDate = 2 Mar 2008 08:30:10.999
theDate = theDate.AddTicks(10000); // theDate = 2 Mar 2008 08:30:11
3 January 2007