DateTime.Compare() Method in C#

This method is used to compare two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance.

Syntax:

public static int Compare (DateTime t1, DateTime t2);

Parameters:

  • t1: The first object to compare.
  • t2: The second object to compare.

Return Value: This method returns a signed number indicating the relative values of t1 and t2.

Less than zero : If t1 is earlier than t2.
Zero : If t1 is the same as t2.
Greater than zero : If t1 is later than t2.



Below programs illustrate the use of DateTime.Compare(DateTime, DateTime) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.Compare(DateTime,
// DateTime) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating object of DateTime
        DateTime date1 = new DateTime(2010, 1, 
                                 1, 4, 0, 15);
  
        // creating object of DateTime
        DateTime date2 = new DateTime(2010, 1,
                                 1, 4, 0, 14);
  
        // comparing date1 and date2
        // using Compare() method;
        int value = DateTime.Compare(date1, date2);
  
        // checking
        if (value > 0)
            Console.Write("date1 is later than date2. ");
        else if (value < 0)
            Console.Write("date1 is earlier than date2. ");
        else
            Console.Write("date1 is the same as date2. ");
    }
}

chevron_right


Output:

date1 is later than date2.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.Compare(DateTime, 
// DateTime) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // calling check() method
        check(new DateTime(2010, 1, 3, 4, 0, 15),
              new DateTime(2010, 1, 4, 4, 0, 15));
  
        check(new DateTime(2010, 1, 5, 4, 0, 15),
              new DateTime(2010, 1, 4, 4, 0, 15));
  
        check(new DateTime(2010, 1, 5, 4, 0, 15),
              new DateTime(2010, 1, 5, 4, 0, 15));
    }
  
    public static void check(DateTime date1,
                            DateTime date2)
    {
  
        // comparing date1 and date2
        // using Compare() method;
        int value = DateTime.Compare(date1, date2);
  
        // checking
        if (value > 0)
            Console.WriteLine(" {0:d} is later than {1:d}. ",
                                               date1, date2);
        else if (value < 0)
            Console.WriteLine(" {0:d} is earlier than {1:d}. ",
                                                 date1, date2);
        else
            Console.WriteLine(" {0:d} is the same as {1:d}. ",
                                                date1, date2);
    }
}

chevron_right


Output:

01/03/2010 is earlier than 01/04/2010. 
01/05/2010 is later than 01/04/2010. 
01/05/2010 is the same as 01/05/2010.

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.