Subtraction Operator

TimeSpan.Subtraction Operator (TimeSpan, TimeSpan)

 
equivalentCodeEntityM:System.TimeSpan.Subtract(System.TimeSpan)

Subtracts a specified TimeSpan from another specified TimeSpan.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public static TimeSpan operator -(
	TimeSpan t1,
	TimeSpan t2
)

Parameters

t1
Type: System.TimeSpan

The minuend.

t2
Type: System.TimeSpan

The subtrahend.

Return Value

Type: System.TimeSpan

An 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 4.5
.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
Return to top
Show:
© 2016 Microsoft