C# DateTimeOffset CompareTo

Description

DateTimeOffset CompareTo 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.

Syntax

DateTimeOffset.CompareTo has the following syntax.


public int CompareTo(
  DateTimeOffset other
)

Parameters

DateTimeOffset.CompareTo has the following parameters.

  • other - An object to compare with the current DateTimeOffset object.

Returns

DateTimeOffset.CompareTo method returns A signed integer that indicates the relationship between the current DateTimeOffset object and other, as the following table shows. Return Value Description Less than zero The current DateTimeOffset object is earlier than other. Zero The current DateTimeOffset object is the same as other. Greater than zero. The current DateTimeOffset object is later than other.

Example

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


/*w w w  .j ava  2s  .co m*/
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));

        
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version