DateTimeOffset.Compare(DateTimeOffset, DateTimeOffset) DateTimeOffset.Compare(DateTimeOffset, DateTimeOffset) DateTimeOffset.Compare(DateTimeOffset, DateTimeOffset) DateTimeOffset.Compare(DateTimeOffset, DateTimeOffset) Method

定義

2 つの DateTimeOffset オブジェクトを比較し、1 つ目のオブジェクトが 2 つ目のオブジェクトの前か後か、または等しいかを示します。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

パラメーター

first
DateTimeOffset DateTimeOffset DateTimeOffset DateTimeOffset

比較する最初のオブジェクト。The first object to compare.

second
DateTimeOffset DateTimeOffset DateTimeOffset DateTimeOffset

比較する 2 番目のオブジェクト。The second object to compare.

戻り値

first パラメーターの値が second パラメーターの値よりも前か後か、または等しいかを示す符号付き整数。次の表を参照してください。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.

戻り値Return value 説明Meaning
0 より小さい値Less than zero firstsecond よりも前の日時です。first is earlier than second.
0Zero firstsecond が等価です。first is equal to second.
0 より大きい値Greater than zero firstsecond より後の日時です。first is later than second.

次の例は、オブジェクトをCompare比較DateTimeOffsetするメソッドの呼び出しを示しています。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

注釈

比較を実行する場合、メソッドは比較をfirst実行するsecond前に、パラメーターとパラメーターの両方を世界協定時刻 (UTC) に変換します。In performing the comparison, the method converts both the first and the second parameters to Coordinated Universal Time (UTC) before it performs the comparison. メソッドは、次の場合と同じです。The method is equivalent to the following:

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

つまり、メソッドはCompare 、2つDateTimeOffsetのオブジェクトが1つの時点を表すかどうかを判断します。In other words, the Compare method determines whether two DateTimeOffset objects represent a single point in time. 日付、時刻、およびオフセットは直接比較されません。It directly compares neither dates and times nor offsets.

適用対象