TimeSpan.Subtraction(TimeSpan, TimeSpan) 运算符

定义

从另一个指定的 TimeSpan 中减去指定的 TimeSpanSubtracts a specified TimeSpan from another specified TimeSpan.

public:
 static TimeSpan operator -(TimeSpan t1, TimeSpan t2);
public static TimeSpan operator - (TimeSpan t1, TimeSpan t2);
static member ( - ) : TimeSpan * TimeSpan -> TimeSpan
Public Shared Operator - (t1 As TimeSpan, t2 As TimeSpan) As TimeSpan

参数

t1
TimeSpan

被减数。The minuend.

t2
TimeSpan

减数。The subtrahend.

返回

TimeSpan

一个对象,其值是 t1 的值减去 t2 的值后所得的结果。An object whose value is the result of the value of t1 minus the value of t2.

例外

返回值小于 MinValue 或大于 MaxValueThe return value is less than MinValue or greater than MaxValue.

示例

下面的示例使用 TimeSpan 减法运算符来计算每周工作日的总长度。The following example uses the TimeSpan subtraction operator to calculate the total length of the weekly work day. 它还使用 TimeSpan 加法运算符来计算每日休息时间的总时间,然后将其用于减法运算,计算实际的每日总工作时间。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.

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
Module Example
   Public Sub Main()
      Dim startWork As New TimeSpan(08,00,00)
      Dim endWork As New TimeSpan(18,30,00)
      Dim lunchBreak As New TimeSpan(1, 0, 0)
      Dim breaks As New TimeSpan(0, 30, 0)
      
      Console.WriteLine("Length of work day: {0}", 
                        endWork - startWork)
      Console.WriteLine("Actual time worked: {0}",
                        endwork - startwork - (lun\chBreak + breaks))                  
   End Sub
End Module
' The example displays the following output:
'     Length of work day: 10:30:00
'     Actual time worked: 09:00:00

注解

此运算符的等效方法是 TimeSpan.Subtract(TimeSpan)The equivalent method for this operator is TimeSpan.Subtract(TimeSpan)

适用于