Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DateTimeOffset.Compare Method

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)
public static int Compare(
	DateTimeOffset first,
	DateTimeOffset second
)

Parameters

first
Type: System.DateTimeOffset
The first object to compare.
second
Type: System.DateTimeOffset
The second object to compare.

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

Meaning

Less than zero

first is earlier than second.

Zero

first is equal to second.

Greater than zero

first is later than second.

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


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.

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         
   }
}


.NET Framework

Supported in: 4, 3.5 SP1, 3.0 SP1, 2.0 SP1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.