DateTimeOffset.Compare(DateTimeOffset, DateTimeOffset) 메서드

정의

DateTimeOffset 개체를 비교하고 첫 번째 개체가 두 번째 개체보다 빠른지, 같은지, 늦은지 알려 줍니다.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

비교할 첫 번째 개체입니다.The first object to compare.

second
DateTimeOffset

비교할 두 번째 개체입니다.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

설명

비교를 수행 하는 동안 메서드는 비교를 수행 하기 전에 firstsecond 매개 변수를 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 메서드는 두 개의 DateTimeOffset 개체가 한 시점을 나타내는지 여부를 확인 합니다.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.

적용 대상