Comparing only Date in the DateTime datatype in C#


This content was proof-read by Grammarly. You can Try Grammary for FREE

The DateTime datatype includes both the date as well as the time component to it in C#. By any chance , if you want to compare only the date part in the DateTime variable in C# , you can use the Date property defined in the DateTime class.

How to compare only Date in the DateTime datatype in C# ?

using System;

namespace AbundantcodeConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt1 = new DateTime(2011, 1, 1);
            DateTime dt2 = DateTime.Now;
            if(dt1.Date != dt2.Date)
                Console.WriteLine("Dates are not equal");
            Console.ReadLine();
        }
    }
}
%d bloggers like this: