DateTimeOffset.CompareTo(DateTimeOffset) DateTimeOffset.CompareTo(DateTimeOffset) DateTimeOffset.CompareTo(DateTimeOffset) DateTimeOffset.CompareTo(DateTimeOffset) Method

定義

現在DateTimeOffset、指定されたオブジェクトDateTimeOffsetオブジェクトし、または 2 番目より後で、現在のオブジェクトが以前より、同じかどうかを示すDateTimeOffsetオブジェクト。Compares the current DateTimeOffset object to a specified DateTimeOffset object and indicates whether the current object is earlier than, the same as, or later than the second DateTimeOffset object.

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 DateTimeOffset

現在の DateTimeOffset オブジェクトと比較するオブジェクト。An object to compare with the current DateTimeOffset object.

戻り値

現在の間のリレーションシップを示す符号付き整数DateTimeOffsetオブジェクトとother次の表に示すように、します。A signed integer that indicates the relationship between the current DateTimeOffset object and other, as the following table shows.

戻り値Return Value 説明Description
0 より小さい値Less than zero 現在DateTimeOffsetオブジェクトが以前よりもotherします。The current DateTimeOffset object is earlier than other.
0Zero 現在DateTimeOffsetオブジェクトが同じotherします。The current DateTimeOffset object is the same as other.
0 より大きい値Greater than zero. 現在DateTimeOffsetオブジェクトがよりも後otherします。The current DateTimeOffset object is later than other.

実装

次の例への呼び出し、CompareToメソッドを比較するDateTimeOffsetオブジェクト。The following example illustrates calls to the CompareTo 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) 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         
   }
}
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 ; の値は、そのかどうかを 2 つのオブジェクトの 1 つの時点を表して、現在のオブジェクトよりも前、後かどうかや、と同じことを示しますotherパラメーター。This method compares DateTimeOffset objects by comparing their UtcDateTime values; that is, it determines whether the two objects represent a single point in time, and indicates whether the current object is earlier than, later than, or the same as the other parameter.

適用対象