語言

TimeSpan.Subtraction(TimeSpan, TimeSpan) 運算子

定義

從另一個指定 TimeSpanTimeSpan中減去 。

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

最重要。

t2
TimeSpan

副底線。

傳回

一個物件,其值是 的 t1 值減去 的 t2值的結果。

例外狀況

回傳值小於 TimeSpan.MinValue ,或大於 TimeSpan.MaxValue。

範例

以下範例使用 TimeSpan 減法運算子計算每週工作日的總長度。 它也使用 TimeSpan 加法算子計算每日休息的總時間,然後再用減法運算計算實際每日工作總時間。

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
let startWork = TimeSpan(08,00,00)
let endWork = TimeSpan(18,30,00)
let lunchBreak = TimeSpan(1, 0, 0)
let breaks = TimeSpan(0, 30, 0)

printfn $"Length of work day: {endWork - startWork}"
printfn $"Actual time worked: {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)

適用於