Condividi tramite


DateTimeOffset.Compare Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Compares two DateTimeOffset objects and indicates whether the first is earlier than the second, equal to the second, or later than the second.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function Compare ( _
    first As DateTimeOffset, _
    second As DateTimeOffset _
) As Integer
public static int Compare(
    DateTimeOffset first,
    DateTimeOffset second
)

Parameters

Return Value

Type: System.Int32
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

Description

Less than zero

first is earlier than second.

Zero

first is equal to second.

Greater than zero

first is later than second.

Remarks

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);

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.

Examples

The following example illustrates calls to the Compare method to compare DateTimeOffset objects.

Module Example
   Private Enum TimeComparison As Integer
      Earlier = -1
      Same = 0
      Later = 1
   End Enum

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim firstTime As New DateTimeOffset(#9/1/2007 6:45:00 AM#, _
                       New TimeSpan(-7, 0, 0))

      Dim secondTime As DateTimeOffset = firstTime
      outputBlock.Text += String.Format("Comparing {0} and {1}: {2}", _
                        firstTime, secondTime, _
                        CType(DateTimeOffset.Compare(firstTime, secondTime),  _
                              TimeComparison)) + vbCrLf

      secondTime = New DateTimeOffset(#9/1/2007 6:45:00 AM#, _
                       New TimeSpan(-6, 0, 0))
      outputBlock.Text += String.Format("Comparing {0} and {1}: {2}", _
                       firstTime, secondTime, _
                       CType(DateTimeOffset.Compare(firstTime, secondTime),  _
                             TimeComparison)) + vbCrLf

      secondTime = New DateTimeOffset(#9/1/2007 8:45:00 AM#, _
                       New TimeSpan(-5, 0, 0))
      outputBlock.Text += String.Format("Comparing {0} and {1}: {2}", _
                       firstTime, secondTime, _
                       CType(DateTimeOffset.Compare(firstTime, secondTime),  _
                             TimeComparison)) + vbCrLf
      ' The example displays the following output:
      '       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
using System;

public class Example
{
   private enum TimeComparison
   {
      Earlier = -1,
      Same = 0,
      Later = 1
   };

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      DateTimeOffset firstTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0,
                                 new TimeSpan(-7, 0, 0));

      DateTimeOffset secondTime = firstTime;
      outputBlock.Text += String.Format("Comparing {0} and {1}: {2}",
                        firstTime, secondTime,
                        (TimeComparison)DateTimeOffset.Compare(firstTime, secondTime)) + "\n";

      secondTime = new DateTimeOffset(2007, 9, 1, 6, 45, 0,
                       new TimeSpan(-6, 0, 0));
      outputBlock.Text += String.Format("Comparing {0} and {1}: {2}",
                       firstTime, secondTime,
                       (TimeComparison)DateTimeOffset.Compare(firstTime, secondTime)) + "\n";

      secondTime = new DateTimeOffset(2007, 9, 1, 8, 45, 0,
                       new TimeSpan(-5, 0, 0));
      outputBlock.Text += String.Format("Comparing {0} and {1}: {2}",
                       firstTime, secondTime,
                       (TimeComparison)DateTimeOffset.Compare(firstTime, secondTime)) + "\n";
      // The example displays the following output:
      //       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         
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.