C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: We see that the difference between Christmas (December 25) and New Year's Day (January 1) is seven days at the end of 2008.
Info: Note how the DateTime constructors are passed three arguments. These specify the year, the month, and the day.
Negative TimeSpans: If you subtract a later date from an earlier date, you can receive a negative TimeSpan value.
Note: For example, change the "newYears" parameters to be the year 2007, and you will get -724 days (negative time).
C# program that uses DateTime.Subtract
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
And: This can be implemented using low-level intermediate language. Please look inside Subtract with IL Disassembler.
IL Disassembler Tutorial