DateTimeOffset.CompareTo() Method in C#

DateTimeOffset.CompareTo(DateTimeOffset) Method is used to compare 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: public int CompareTo (DateTimeOffset other);
Here, it takes an object to compare with the current DateTimeOffset object.

Return Value: This method returns a signed integer that indicates the relationship between the current DateTimeOffset object and other.

  • Less than zero: Means the current DateTimeOffset object is earlier than other.
  • Zero: Means the current DateTimeOffset object is the same as other.
  • Greater than zero: Means the current DateTimeOffset object is later than other.

Below programs illustrate the use of DateTimeOffset.CompareTo(DateTimeOffset) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTimeOffset.CompareTo(DateTimeOffset)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating object of  DateTimeOffset
        DateTimeOffset offset1 = new DateTimeOffset(2007,
                 6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
  
        // creating object of  DateTimeOffset
        DateTimeOffset offset2 = new DateTimeOffset(2006,
                 6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
  
        // comparing two offset1 and offset2
        // instance using CompareTo() method
        int value = offset1.CompareTo(offset2);
  
        if (value > 0) 
        {
            Console.Write("offset1 is later than offset2 ");
        }
        else if (value < 0) 
        {
            Console.Write("offset1 is earlier than offset2");
        }
        else 
        {
            Console.Write("offset1 is equal to offset2");
        }
    }
}

chevron_right


Output:

offset1 is later than offset2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTimeOffset.CompareTo(DateTimeOffset)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating object of  DateTimeOffset
        DateTimeOffset offset1 = new DateTimeOffset(2006,
                 6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
  
        // creating object of  DateTimeOffset
        DateTimeOffset offset2 = new DateTimeOffset(2006,
                 6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
  
        // comparing two offset1 and offset2
        // instance using CompareTo() method
        int value = offset1.CompareTo(offset2);
  
        if (value > 0) 
        {
            Console.Write("offset1 is later than offset2 ");
        }
        else if (value < 0) 
        {
            Console.Write("offset1 is earlier than offset2");
        }
        else 
        {
            Console.Write("offset1 is equal to offset2");
        }
    }
}

chevron_right


Output:

offset1 is equal to offset2

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.