共用方式為


DateTimeOffset.Compare(DateTimeOffset, DateTimeOffset) 方法

定義

比較兩個 DateTimeOffset 物件,並指出第一個日期是早於、等於還是晚於第二個日期。

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

參數

first
DateTimeOffset

要比較的第一個物件。

second
DateTimeOffset

要比較的第二個物件。

傳回

Int32

帶正負號的整數,表示 first 參數的值是早於、晚於或等於 second 參數的值,如下表所示。

傳回值 意義
小於零 first 早於 second
first 等於 second
大於零 first 晚於 second

範例

下列範例說明對 方法的 Compare 呼叫,以比較 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) 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
   }
}
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}: {DateTimeOffset.Compare(firstTime, secondTime) |> enum<TimeComparison>}"

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

let fourthTime = DateTimeOffset(2007, 9, 1, 8, 45, 0, TimeSpan(-5, 0, 0))
printfn $"Comparing {firstTime} and {fourthTime}: {DateTimeOffset.Compare(firstTime, 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(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

備註

在執行比較時,方法會將 和 second 參數轉換成 first 國際標準時間 (UTC) ,然後再執行比較。 方法相當於下列專案:

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

換句話說, Compare 方法會判斷兩 DateTimeOffset 個 物件是否代表單一時間點。 它會直接比較日期和時間和位移。

適用於