TimeSpan.Subtraction Operator (TimeSpan, TimeSpan)
.NET Framework (current version)
Assembly: mscorlib (in mscorlib.dll)
Return Value
Type: System.TimeSpanAn object whose value is the result of the value of t1 minus the value of t2.
| Exception | Condition |
|---|---|
| OverflowException | The return value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
The following example uses the TimeSpan subtraction operator to calculate the total length of the weekly work day. It also uses the TimeSpan addition operator to compute the total time of the daily breaks before using it in a subtraction operation to compute the total actual daily working time.
using System; public class Example { public static void Main() { var startWork = new TimeSpan(08,00,00); var endWork = new TimeSpan(18,30,00); var lunchBreak = new TimeSpan(1, 0, 0); var breaks = new TimeSpan(0, 30, 0); Console.WriteLine("Length of work day: {0}", endWork - startWork); Console.WriteLine("Actual time worked: {0}", endWork - startWork - (lunchBreak + breaks)); } } // The example displays the following output: // Length of work day: 10:30:00 // Actual time worked: 09:00:00
Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Show: