A DateTime instance represents a specific time (such as 4:15 A.M. on September 5, 1970), whereas a TimeSpan instance represents a period of time (such as 2 hours, 35 minutes). You will often want to add, subtract, and compare TimeSpan and DateTime instances.
Internally, both DateTime and TimeSpan use ticks to represent time—a tick is equal to 100 nanoseconds. TimeSpan stores its time interval as the number of ticks equal to that interval, and DateTime stores time as the number of ticks since 12:00:00 midnight on January 1 in 0001 C.E. (C.E. stands for Common Era and is equivalent to A.D. in the Gregorian Calendar.) This approach and the use of operator overloading makes it easy for DateTime and TimeSpace to support basic arithmetic and comparison operations. Table2.4 summarizes the operator support provided by the DateTime and TimeSpan structures.
Operator | TimeSpan | DateTime |
---|---|---|
Assignment (=) | Because TimeSpan is a structure, assignment returns a copy and not a reference. | Because DateTime is a structure, assignment returns a copy and not a reference. |
Addition (+) | Adds two TimeSpan instances. | Adds a TimeSpan to a DateTime. |
Subtraction (-) | Subtracts one TimeSpan instance from another. | Subtracts a TimeSpan or a DateTime from a DateTime. |
Equality (==) | Compares two TimeSpan instances and returns true if they are equal. | Compares two DateTime instances and returns true if they are equal. |
Inequality (!=) | Compares two TimeSpan instances and returns true if they aren't equal. | Compares two DateTime instances and returns true if they aren't equal. |
Greater Than (>) | Determines if one TimeSpan is greater than another TimeSpan. | Determines if one DateTime is greater than another DateTime. |
Greater Than or Equal (>=) | Determines if one TimeSpan is greater than or equal to another TimeSpan. | Determines if one DateTime is greater than or equal to another DateTime. |
Less Than (<) | Determines if one TimeSpan is less than another TimeSpan. | Determines if one DateTime is less than another DateTime. |
Less Than or Equal (<=) | Determines if one TimeSpan is less than or equal to another TimeSpan. | Determines if one DateTime is less than or equal to another DateTime. |
Unary Negation (-) | Returns a TimeSpan with a negated value of the specified TimeSpan. | Not Supported. |
Unary Plus (+) | Returns the TimeSpan specified. | Not Supported. |
The DateTime structure also implements the methods AddTicks, AddMilliseconds, AddSeconds, AddMinutes, AddHours, AddDays, AddMonths, and AddYears. Each of these methods allows you to add (or subtract using negative values) the appropriate element of time to a DateTime instance. These methods and the operators listed in Table 2.4 don't modify the original DateTime— instead they create a new instance with the modified value. The following code demonstrates the use of operators to manipulate the DateTime and TimeSpan structures.
// Create a TimeSpan representing 2.5 days TimeSpan timespan1 = new TimeSpan(2,12,0,0); // Create a TimeSpan representing 4.5 days TimeSpan timespan2 = new TimeSpan(4,12,0,0); // Create a TimeSpan representing 1 week TimeSpan oneWeek = timespan1 + timespan2; // Create a DateTime with the current date and time DateTime now = DateTime.Now; // Create a DateTime representing 1 week ago DateTime past = now - oneWeek; // Create a DateTime representing 1 week in the future DateTime future = now + oneWeek;
No comments:
Post a Comment