Open In App

DateTime.Compare() Method in C#

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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. ");
    }
}


Output:

date1 is later than date2.

Example 2:




// 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);
    }
}


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:



Last Updated : 22 Jan, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads