C# DateTimeOffset Compare

Description

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

Syntax

DateTimeOffset.Compare has the following syntax.


public static int Compare(
  DateTimeOffset first,
  DateTimeOffset second
)

Parameters

DateTimeOffset.Compare has the following parameters.

  • first - The first object to compare.
  • second - The second object to compare.

Returns

DateTimeOffset.Compare method returns 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.

Example

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


// ww w .j  av a2 s.c o m
using System;

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

   public static void Main()
   {
      DateTimeOffset firstTime = new DateTimeOffset(2014, 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));

      
   }
}

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