DateTime.CompareTo() Method in C#

This method is used to compare the value of this instance to a specified DateTime value and indicates whether this instance is earlier than, the same as, or later than the specified DateTime value. There are two methods in the overload list of this method as follows:

  • CompareTo(DateTime)
  • CompareTo(Object)


CompareTo(DateTime) Method

This method is used to compare the value of this instance to a specified DateTime value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.

Syntax:

public int CompareTo (DateTime value);

Here, the parameter value is the object to compare to the current instance.

Return Value: This method return a signed number indicating the relative values of this instance and the value parameter.

  • Less than zero : If this instance is earlier than value.
  • Zero : If this instance is the same as value.
  • Greater than zero : If this instance is later than value.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to demonstrate
// CompareTo(DateTime) function
using System;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Taking two DateTime Objects
        DateTime d1 = new DateTime(2018, 12,
                              31, 16, 0, 0);
  
        DateTime d2 = new DateTime(2018, 12,
                              31, 20, 0, 0);
  
        // Using the method
        int res = d1.CompareTo(d2);
  
        string r;
  
        if (res > 0)
            r = "is later than";
  
        else if (res == 0)
            r = "is the same time as";
        else
            r = "is earlier than";
  
        Console.WriteLine("{0} {1} {2}", d1, r, d2);
    }
}

chevron_right


Output:

12/31/2018 16:00:00 is earlier than 12/31/2018 20:00:00
CompareTo(Object) Method

This method is used to compare the value of this instance to a specified object that contains a specified DateTime value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.

Syntax:

public int CompareTo (object value);

Here, the parameter value is the boxed object to compare, or null.

Return Value: This method return a signed number indicating the relative values of this instance and the value parameter.

  • Less than zero : If this instance is earlier than value.
  • Zero : If this instance is the same as value.
  • Greater than zero : If this instance is later than value.

Exception: This method will give ArgumentException if the value is not a DateTime.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to demonstrate
// CompareTo(Object) function
using System;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Taking a DateTime Object
        DateTime d1 = new DateTime(2018, 12,
                              31, 16, 0, 0);
                                
        // Using the method
        // Here, "DateTime.Today" is
        // the property of DateTime struct
        int res = d1.CompareTo(DateTime.Today);
  
        string r;
  
        if (res > 0)
            r = "is later than";
  
        else if (res == 0)
            r = "is the same time as";
        else
            r = "is earlier than";
  
        Console.WriteLine("{0} {1} {2}", d1, r, 
                                DateTime.Today);
    }
}

chevron_right


Output:

12/31/2018 16:00:00 is earlier than 01/21/2019 00:00:00

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.