How to compare only Date without Time in DateTime datatype in C#?


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

There are times when you might want to compare only the date parts of two DateTime variable in C#.

How to compare only Date without Time in DateTime datatype in C#?

You can easily compare using the Date property from the DateTime object.

using System;
namespace AbundantcodeCsharpSample
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime date1 = DateTime.Now;
            DateTime date2 = DateTime.Now.AddDays(-50);

            if (date1.Date == date2.Date)
                Console.WriteLine("Both the dates are same");
        }
    
    }
  
}

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

%d bloggers like this: