TimeSpan.Subtraction(TimeSpan, TimeSpan) 演算子

定義

指定したもう 1 つの TimeSpan から、指定した 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

被減数。

t2
TimeSpan

減数。

戻り値

t2 の値から t1 の値を減算した結果を値とするオブジェクト。

例外

戻り値が TimeSpan.MinValue より小さいか、 TimeSpan.MaxValue より大きい。

次の例では、減算演算子を TimeSpan 使用して、週単位の稼働日の合計長を計算します。 また、加算演算子を TimeSpan 使用して 1 日の休憩の合計時間を計算してから、減算演算で使用して、実際の 1 日の作業時間の合計を計算します。

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)

適用対象