DateTimeOffset.Compare(DateTimeOffset, DateTimeOffset) Método

Definición

Compara dos objetos DateTimeOffset e indica si el primero es anterior, igual o posterior al segundo.Compares two DateTimeOffset objects and indicates whether the first is earlier than the second, equal to the second, or later than the second.

public:
 static int Compare(DateTimeOffset first, DateTimeOffset second);
public static int Compare (DateTimeOffset first, DateTimeOffset second);
static member Compare : DateTimeOffset * DateTimeOffset -> int
Public Shared Function Compare (first As DateTimeOffset, second As DateTimeOffset) As Integer

Parámetros

first
DateTimeOffset

Primer objeto que se va a comparar.The first object to compare.

second
DateTimeOffset

Segundo objeto que se va a comparar.The second object to compare.

Devoluciones

Int32

Entero con signo que indica si el valor del parámetro first es anterior, posterior o igual a la hora que el valor del parámetro second, como se muestra en la tabla siguiente.A signed integer that indicates whether the value of the first parameter is earlier than, later than, or the same time as the value of the second parameter, as the following table shows.

Valor devueltoReturn value SignificadoMeaning
Menor que ceroLess than zero first es anterior a second.first is earlier than second.
CeroZero first es igual a second.first is equal to second.
Mayor que ceroGreater than zero first es posterior a second.first is later than second.

Ejemplos

En el ejemplo siguiente se muestran las llamadas al Compare método para comparar los DateTimeOffset objetos.The following example illustrates calls to the Compare method to compare DateTimeOffset objects.

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) DateTimeOffset.Compare(firstTime, 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) DateTimeOffset.Compare(firstTime, 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) DateTimeOffset.Compare(firstTime, 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
   }
}
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(DateTimeOffset.Compare(firstTime, 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(DateTimeOffset.Compare(firstTime, 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(DateTimeOffset.Compare(firstTime, 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

Comentarios

Al realizar la comparación, el método convierte los first parámetros y en la second hora universal coordinada (UTC) antes de llevar a cabo la comparación.In performing the comparison, the method converts both the first and the second parameters to Coordinated Universal Time (UTC) before it performs the comparison. El método es equivalente a lo siguiente:The method is equivalent to the following:

return DateTime.Compare(first.UtcDateTime, second.UtcDateTime);
Return DateTime.Compare(first.UtcDateTime, second.UtcDateTime)

En otras palabras, el Compare método determina si dos DateTimeOffset objetos representan un único punto en el tiempo.In other words, the Compare method determines whether two DateTimeOffset objects represent a single point in time. No se comparan directamente ninguna fecha y hora ni desplazamientos.It directly compares neither dates and times nor offsets.

Se aplica a