DateTimeOffset.CompareTo(DateTimeOffset) Méthode

Définition

Compare l'objet DateTimeOffset en cours à un objet DateTimeOffset spécifié et indique si l'objet en cours est antérieur, égal ou ultérieur au second objet DateTimeOffset.

public:
 virtual int CompareTo(DateTimeOffset other);
public int CompareTo (DateTimeOffset other);
abstract member CompareTo : DateTimeOffset -> int
override this.CompareTo : DateTimeOffset -> int
Public Function CompareTo (other As DateTimeOffset) As Integer

Paramètres

other
DateTimeOffset

Objet à comparer à l’objet DateTimeOffset actuel.

Retours

Entier signé indiquant la relation entre l'objet DateTimeOffset en cours et other, comme l'indique le tableau suivant.

Valeur de retour Description
Inférieure à zéro L'objet DateTimeOffset en cours est antérieur à other.
Zéro L'objet DateTimeOffset en cours est identique à other.
Supérieure à zéro. L'objet DateTimeOffset en cours est ultérieur à other.

Implémente

Exemples

L’exemple suivant illustre les appels à la CompareTo méthode pour comparer des DateTimeOffset objets.

using System;

public class CompareTimes
{
   private enum TimeComparison
   {
      Earlier = -1,
      Same = 0,
      Later = 1
   };

   public static void Main()
   {
      DateTimeOffset firstTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0,
                                 new TimeSpan(-7, 0, 0));

      DateTimeOffset secondTime = firstTime;
      Console.WriteLine("Comparing {0} and {1}: {2}",
                        firstTime, secondTime,
                        (TimeComparison) firstTime.CompareTo(secondTime));

      secondTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0,
                       new TimeSpan(-6, 0, 0));
      Console.WriteLine("Comparing {0} and {1}: {2}",
                       firstTime, secondTime,
                       (TimeComparison) firstTime.CompareTo(secondTime));

      secondTime = new DateTimeOffset(2007, 9, 1, 8, 45, 0,
                       new TimeSpan(-5, 0, 0));
      Console.WriteLine("Comparing {0} and {1}: {2}",
                       firstTime, secondTime,
                       (TimeComparison) firstTime.CompareTo(secondTime));
      // The example displays the following output to the console:
      //       Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -07:00: Same
      //       Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -06:00: Later
      //       Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 8:45:00 AM -05:00: Same
   }
}
open System

type TimeComparison =
    | Earlier = -1
    | Same = 0
    | Later = 1

let firstTime = DateTimeOffset(2007, 9, 1, 6, 45, 0, TimeSpan(-7, 0, 0))

let secondTime = firstTime
printfn $"Comparing {firstTime} and {secondTime}: {firstTime.CompareTo secondTime |> enum<TimeComparison>}"

let thirdTime = DateTimeOffset(2007, 9, 1, 6, 45, 0, TimeSpan(-6, 0, 0))
printfn $"Comparing {firstTime} and {thirdTime}: {firstTime.CompareTo thirdTime |> enum<TimeComparison>}"

let fourthTime = new DateTimeOffset(2007, 9, 1, 8, 45, 0,
                new TimeSpan(-5, 0, 0))
printfn $"Comparing {firstTime} and {fourthTime}: {firstTime.CompareTo fourthTime |> enum<TimeComparison>}" 
                
// The example displays the following output to the console:
//       Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -07:00: Same
//       Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -06:00: Later
//       Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 8:45:00 AM -05:00: Same
Module CompareTimes
   Private Enum TimeComparison As Integer
      Earlier = -1
      Same = 0
      Later = 1
   End Enum
      
   Public Sub Main()
      Dim firstTime As New DateTimeOffset(#09/01/2007 6:45:00AM#, _
                       New TimeSpan(-7, 0, 0))
  
      Dim secondTime As DateTimeOffset = firstTime
      Console.WriteLine("Comparing {0} and {1}: {2}", _
                        firstTime, secondTime, _
                        CType(firstTime.CompareTo(secondTime), _
                              TimeComparison))

      secondTime = New DateTimeOffset(#09/01/2007 6:45:00AM#, _
                       New TimeSpan(-6, 0, 0))      
      Console.WriteLine("Comparing {0} and {1}: {2}", _
                       firstTime, secondTime, _
                       CType(firstTime.CompareTo(secondTime), _
                             TimeComparison))
      
      secondTime = New DateTimeOffset(#09/01/2007 8:45:00AM#, _
                       New TimeSpan(-5, 0, 0))
      Console.WriteLine("Comparing {0} and {1}: {2}", _
                       firstTime, secondTime, _
                       CType(firstTime.CompareTo(secondTime), _
                             TimeComparison))
      ' The example displays the following output to the console:
      '       Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -07:00: Same
      '       Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 6:45:00 AM -06:00: Later
      '       Comparing 9/1/2007 6:45:00 AM -07:00 and 9/1/2007 8:45:00 AM -05:00: Same      
   End Sub
End Module

Remarques

Cette méthode compare les DateTimeOffset objets en comparant leurs UtcDateTime valeurs, c’est-à-dire qu’elle détermine si les deux objets représentent un point unique dans le temps et indique si l’objet actuel est antérieur, postérieur à ou identique au other paramètre .

S’applique à