Comparing only Date in the DateTime datatype in C#

Published on September 8, 2016 by abundantcode

Filed under C#

Last modified September 8, 2016

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 400 times

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

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


You might be Interested in these Posts

Displaying all the worksheet names in Excel using VBA Code

Do you want to display the names of the worksheets in the excel workbook ?. Here;s how you can do it...

How to change positive numbers to negative in Microsoft Excel 2016 ?

Do you want to change the positive numbers to negative numbers and vice versa (or in simple terms) ,...

How to convert number to text in Excel?

In Microsoft Excel 2016 , there are times when you don't want the numbers present in the cell to be ...