Gali
Gali

Reputation: 14963

problem with Issue dates

i have this in my C# program:

if (DateTime.Now >= myDateTime1 && DateTime.Now <= myDateTime2)
            {
                return true;
            }
            else
            {
                return false;
            }

this are the values:

DateTime.Now = 20/06/11 10:55:43

myDateTime1  = 20/06/11 11:29:09

myDateTime2  = 21/06/11 11:31:07

but why this sentence return me False ?

EDIT: I need to compare only Dates, not with time.

Upvotes: 0

Views: 66

Answers (4)

asma
asma

Reputation: 2835

Because DateTime.Now is Less than myDateTime1. First condition gets false and next condition won't run. If have any query still, you can ask or correct me if i'm wrong.

Upvotes: 1

Andrea
Andrea

Reputation: 894

Because

(20/06/11 10:55:43) >= (20/06/11 11:29:09) 

is false :)

Upvotes: 0

Colin Mackay
Colin Mackay

Reputation: 19185

By the looks of it, DateTime.Now is not between myDateTime1 and myDateTime2, which is what the if statement is requiring, so it returns false.

Upvotes: 0

George Duckett
George Duckett

Reputation: 32438

For the dates you've given, DateTime.Now is before myDateTime1, so DateTime.Now >= myDateTime1 is evaluating to false, which means the whole if statement is evaluating to false, so it's running the else code, which returns false.

FYI if you set a breakpoint on the if statement (press F9), when the program stops you can hover the mouse over the binary operators (such as <=, && and >=) to show what they evaluate to.

EDIT: If you only want to compare the date, then use if (DateTime.Today >= myDateTime1.Date && DateTime.Today <= myDateTime2.Date)

Upvotes: 2

Related Questions