Differentiate DateTime.Now and DateTime.Now.Date

3

I have a test and would like to know when an attribute has received DateTime.Now or DateTime.Now.Date , how can I differentiate when an attribute has received these values?

Note: It can be a number of values, the purpose is to know when a date is a simple date (without time) and a complete date (with time).

    
asked by anonymous 22.11.2015 / 00:06

2 answers

5

Only by language and .NET, no, because both return DateTime . What you can do is intuit this by checking in your variable whether the number of seconds equals zero ( which is what DateTime.Now.Date seeks to do ):

if (meuDateTime.TimeOfDay.TotalSeconds == 0) 
{
    Console.WriteLine("Aparentemente atribuição veio de DateTime.Now.Date.");
} else {
    Console.WriteLine("Atribuição veio de DateTime.Now.");
}

It is worth noting that this test is not 100% accurate, because nothing prevents DateTime.Now from being fired exactly at the second zero of a given day, but that is a little remote to happen.

    
22.11.2015 / 00:20
1

Yes. First of all I suggested using DateTime.Today today that is equivalent to DateTime.Now.Date .

For this same reason it is enough to make the following comparison:

if(data == data.Date)

EDIT: This comparison also checks the year, month and day

    
22.11.2015 / 00:23