Comparing only the DateTime field date in C #

3

I need to compare only the date of two fields DateTime .

DateTime aux = new DateTime(2016, 09, 02, 10, 0, 0);
if (aux.Equals(DateTime.Now))
{
     //Alguma ação...
}

In the code above, I need to enter if when the date (09/02/2016) is equal on both objects. In this case it does not enter because the Time of the two objects is different. What should I do?

    
asked by anonymous 02.09.2016 / 15:38

2 answers

9

You should get the Date property of the DateTime for example

DateTime aux = new DateTime(2016, 09, 02, 10, 0, 0);
if (aux.Date.Equals(DateTime.Now.Date))
{
     //Alguma ação...
}
    
02.09.2016 / 15:42
4

Marco Giovanni's answer is correct, I decided to respond to put the idiom:

var aux = new DateTime(2016, 09, 02, 10, 0, 0);
if (aux.Date == DateTime.Now.Date) {
    Console.WriteLine("Ok");
}

See working on dotNetFiddle .

    
02.09.2016 / 16:16