
.NET 1.1+DateTime Information Extraction
The twenty-sixth part of the C# Fundamentals tutorial continues the examination of the DateTime data type provided by C# and the .NET Framework. In this instalment we will consider how information can be compared and extracted from values of this type.
DateTime Comparison
Equality and Inequality Operators
Throughout the C# Fundamentals tutorial the equality (==) and inequality (!=) operators have been used to compare values. These operators can be used with the DateTime data type in the same manner as for any other value type.
DateTime startDate = DateTime.Parse("30 Dec 2006");
DateTime endDate = DateTime.Parse("1 Jan 2007");
DateTime targetDate = DateTime.Parse("1 Jan 2007");
bool result;
result = startDate == endDate; // result = false
result = targetDate == endDate; // result = true
result = startDate != endDate; // result = true
result = targetDate != endDate; // result = false
Comparison Operators
The DateTime data type holds information that supports the concept of ordering. This means that, as with the numeric data types, it is possible to use comparison operators to determine which of two operands is the greater (or in the case of date and time information, the later). As with numeric data, the available operators are greater than (>), less than (<), greater than or equal to (>=) and less than or equal to (<=).
DateTime startDate = DateTime.Parse("30 Dec 2006");
DateTime endDate = DateTime.Parse("1 Jan 2007");
DateTime targetDate = DateTime.Parse("1 Jan 2007");
bool result;
result = startDate > endDate; // result = false
result = startDate < endDate; // result = true
result = startDate >= endDate; // result = false
result = startDate <= endDate; // result = true
result = targetDate > endDate; // result = false
result = targetDate < endDate; // result = false
result = targetDate >= endDate; // result = true
result = targetDate <= endDate; // result = true
In addition, the DateTime structure includes the comparison methods, Equals, Compare and CompareTo. These operate in much the same way as for the string class and were described in the earlier article, 'C# String Comparison Functions'.
DateTime Component Extraction
The DateTime data type provides a complex structure of numbers representing a date and time, including a year, month, day, hour, etc. The structure incorporates all of the rules of standard date and time information, automatically dealing with problems such as determining the number of days in each month and working with leap years. This information is readily obtained using the range of available properties and methods.
Extraction of Specific DateTime Components
The DateTime structure holds numeric values for the Year, Month, Day, Hour, Minute Second and Millisecond of a time. Each of these can be extracted independently as an integer using the structure's properties. The following example defines a date and time with an accuracy in milliseconds and demonstrates how the various values are accessed.
DateTime theDate = DateTime.Parse("30 Dec 2006 01:02:03.456 PM");
int year = theDate.Year; // year = 2006
int month = theDate.Month; // month = 12
int day = theDate.Day; // day = 30
int hour = theDate.Hour; // hour = 13
int minute = theDate.Minute; // minute = 2
int second = theDate.Second; // second = 3
int millisecond = theDate.Millisecond; // millisecond = 456
Note that the properties are read-only, allowing the values to be extracted but not modified. However, a new DateTime value can be generated using the extracted values. This is achieved using a constructor as described in the previous article of the C# Fundamentals tutorial.
1 January 2007