DateTimeOffset.CompareTo(DateTimeOffset) 方法

定义

将当前的 DateTimeOffset 对象与指定的 DateTimeOffset 对象进行比较,并指明当前对象是早于、等于还是晚于另一个 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

参数

other
DateTimeOffset

将与当前的 DateTimeOffset 对象进行比较的对象。

返回

一个有符号的整数,它指明了当前的 DateTimeOffset 对象与 other 之间的关系,如下表所示。

返回值 说明
小于零 当前的 DateTimeOffset 对象早于 other
当前的 DateTimeOffset 对象与 other 相同。
大于零。 当前的 DateTimeOffset 对象晚于 other

实现

示例

以下示例演示了对 CompareTo 方法的调用以比较 DateTimeOffset 对象。

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

注解

此方法通过比较对象的值来比较 DateTimeOffset 对象 UtcDateTime ;也就是说,它确定这两个对象是否表示单个时间点,并指示当前对象是早于、晚于还是与 参数相同 other

适用于