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

定義

現在の DateTimeOffset オブジェクトと、指定された DateTimeOffset オブジェクトとを比較し、現在のオブジェクトがもう一方の 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

注釈

このメソッドはDateTimeOffsetUtcDateTime値を比較することによってオブジェクトを比較します。つまり、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.

適用対象