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

Published on February 15, 2017 by abundantcode

Filed under C#

Last modified February 15, 2017

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 2797 times

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

Leave a Comment


x

You might be Interested in these Posts

How to get the AM or PM value from a DateTime object in C# ?

There are times when you might want to get only the string AM or PM from the DateTime object that yo...

Case insensitive string comparison for Contains in C#

When using the string.contains method , the comparison is done based on the exact string that is pas...

C Program to a print pascal triangle

Problem Statement Write a program in C to print pascal triangle. How to print a pascal triange in ...