
One DateTime can be subtracted from another. This returns the difference in time between the two dates. For example, the difference between December 25 and January 1 in the year 2008 is seven days. We receive this value using the DateTime Subtract method.
This C# article shows how to use the DateTime.Subtract method. Subtract computes differences between two dates.
We see how you can use the Subtract method with one argument on the DateTime struct instance. You can use Subtract to get the difference between two dates; you must subtract the smaller date from the larger date. The program shows that the difference between Christmas (December 25) and New Year's Day (January 1) is seven days at the end of 2008.
Note: I checked this on a calendar and it is correct.
Program that uses DateTime.Subtract [C#]
using System;
class Program
{
static void Main()
{
// Create DateTime instances for December 25 and January 1.
// ... Then compute the difference with Subtract.
// ... Write the result.
DateTime christmas = new DateTime(2008, 12, 25);
DateTime newYears = new DateTime(2009, 1, 1);
TimeSpan span = newYears.Subtract(christmas);
Console.WriteLine(span);
Console.WriteLine("{0} days", span.TotalDays);
}
}
Output
7.00:00:00
7 days
Program review. This program introduces the Main entry point and it instantiates two DateTime struct instances. Note how the DateTime constructors are passed three arguments; these specify the year, the month, and the day. Next, it subtracts "christmas" from "newYears".
Negative TimeSpans. If you subtract a later date from an earlier date, you can receive a negative TimeSpan value. For example, change the "newYears" parameters to be the year 2007, and you will get -724 days (negative time).

Implementation. When inspecting the internals of the DateTime class, I saw that each DateTime struct stores a ticks field which is a long type representation of the time.
When using Subtract, the long fields from the two DateTime instances are simply subtracted, which can be implemented using low-level intermediate language. Please look inside Subtract and disassemble this program in IL Disassembler to follow along.
IL Disassembler Tutorial
We saw the DateTime type and its Subtract instance method in the C# language. This method receives one argument and returns a TimeSpan instance indicating the elapsed time. Typically, you want to subtract a smaller and earlier date from the larger date. The program was tested for correctness against a calendar.
Time Representations