find and solve || findandsolve.com
Please wait.....

How to Compare Two Dates in C#

In this C# programming language, the DateTime() constructor is used to create the format of the date. If a conditional statement is used to compare the two dates using DateTime.Compare() Method in C#.

C# program to compare two dates

In this program, you will show how to compare two dates in the C# programming language. You will read the year, month, and day as input from the user for both dates and compare them both.

In the C#DateTime.Compare() method is used for the comparison of two DateTime instances. It returns an integer value,

  • < 0 − If date1 is earlier than date2
  • 0 − If date1 is the same as date2
  • > 0 − If date1 is later than date2


Syntax

public static int Compare (DateTime date1, DateTime date2);


Compare Date Example

using System;
public class Program
{
 public static void Main()
 {
  int firstYear, firstMonth, firstDay, secondYear, secondMonth, secondDate;
  Console.WriteLine("Enter data for the first date : ");
  Console.Write("Year : ");
  firstYear = Convert.ToInt32(Console.ReadLine());
  Console.Write("Month : ");
  firstMonth = Convert.ToInt32(Console.ReadLine());
  Console.Write("Day : ");
  firstDay = Convert.ToInt32(Console.ReadLine());
  Console.WriteLine("Enter data for the second date : ");
  Console.Write("Year : ");
  secondYear = Convert.ToInt32(Console.ReadLine());
  Console.Write("Month : ");
  secondMonth = Convert.ToInt32(Console.ReadLine());
  Console.Write("Day : ");
  secondDate = Convert.ToInt32(Console.ReadLine());
  DateTime dateTime = new DateTime(firstYear, firstMonth, firstDay);
  DateTime dateTime1 = new DateTime(secondYear, secondMonth, secondDate);
  if (dateTime < dateTime1)
  {
   Console.WriteLine("Time {0} is before {1}", dateTime, dateTime1);
   Console.ReadKey();
  }
  else
  {
   Console.WriteLine("Time {0} is after {1}", dateTime, dateTime1);
   Console.ReadKey();
  }
 }
}

Output




Code Explanation 

  • firstYear, firstMonth, firstDay, secondYear, firstMonth, secondDay are integer variables to hold the year, month, and day data for both dates.
  • It takes the inputs from the user and stores them in the variables.
  • Using the user input values, it created two DateTime variables dateTime and dateTime1.
  • The last if-else block compares both dateTime and dateTime1 and prints one message which one comes before.


Compare Two Dates Without Time in C#

In the compare date only in C#, We will learn how to compare two dates only except time in C#. Sometimes, you need to compare only the date parts of two DateTime variables in the C# programming language. You used the == operator and.CompareTo() method to compare the two dates without time in the C# programming language.


Example 1: Using == Operator

using System;
namespace FindAndSolve
{
    public class Program
    {
        /* How to compare two Dates without time in C# */
        static void Main(string[] args)
        {
            DateTime date = DateTime.Now;
            DateTime date1 = DateTime.Now.AddDays(-30);
            if (date.Date == date1.Date)
            {
                Console.WriteLine("Both the dates are same");
            }
            else
            {
                Console.WriteLine("Both the dates are not the same");
            }           
            Console.ReadKey();
        }
    }
}

Output

Both the dates are not the same


Example 2: Using CompareTo() Method

Using CompareTo() method example, you compare the value of this instance to a specified DateTime value and indicate whether this instance is earlier than, the same as, or later than the specified DateTime value.

using System;
namespace FindAndSolve
{
    public class Program     {
        /* How to compare two Dates without time in C# */
        public static void Main(string[] args)
        {
            DateTime date = DateTime.Now;
            DateTime date1 = DateTime.Now.AddDays(-30);
            var compare = date.Date.CompareTo(date1.Date);
            switch (compare)
            {
                case 1:
                    Console.WriteLine("The Date is greater than the Date1.");
                    break;
                case 0:
                    Console.WriteLine("The Date is the same as the Date1.");
                    break;
                default:
                    Console.WriteLine("The Date1 is earlier date than the Date1.");
                    break;
            }
        }
    }
}

Output

The Date is greater than the Date1.