TimeSpan.Subtraction(TimeSpan, TimeSpan) Opérateur

Définition

Soustrait un TimeSpan spécifié d'un autre TimeSpan spécifié.

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

Paramètres

t1
TimeSpan

Diminuende.

t2
TimeSpan

Diminuteur.

Retours

Objet dont la valeur est le résultat de la valeur de t1 moins la valeur de t2.

Exceptions

La valeur de retour est inférieure à TimeSpan.MinValue ou supérieure à TimeSpan.MaxValue.

Exemples

L’exemple suivant utilise l’opérateur TimeSpan de soustraction pour calculer la durée totale du jour de travail hebdomadaire. Il utilise également l’opérateur TimeSpan d’addition pour calculer le temps total des pauses quotidiennes avant de l’utiliser dans une opération de soustraction pour calculer le temps de travail quotidien réel total.

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

Remarques

La méthode équivalente pour cet opérateur est TimeSpan.Subtract(TimeSpan)

S’applique à