curiousBoy
curiousBoy

Reputation: 6834

Classic Asp date comparison not working correctly

I have seen couple similar titles with my question but they were focused on different point.

What I would like to ask is,

I'm trying to compare 2 dates. Yes, sounds super simple!

Here is the issue:

I have two variable and I'm trying to compare this 2 variables with Now().

Just like :

If startDate < Now() and endDate> Now() Then 
do something....
End If

But the thing is, when I compare startDate (which is ('2013-08-08 09:30 AM') with Now() and endDate (which is '2013-09-09 02:30 PM') with Now()

startDate = '2013-08-08 09:30 AM' 'This value comes from db, I have not typed it. Just trying to show  what it has as a  value.

endDate = '2013-09-09 02:30 PM' 'This value comes from db, I have not typed it. Just trying to show what it has as a  value.

and When I compare these 2 date variables:

 If startDate  < Now()  Then 
    do something....
End If

It returns : FALSE. It supposed to return TRUE, isn't it?

If startDate  > Now()  Then 
        do something....
    End If

It returns : TRUE. It supposed to return FALSE, isn't it?

 If endDate  < Now()  Then 
        do something....
    End If

But here, it returns correct. I mean this returns FALSE which is what it supposed to return.

That's so weird. I have checked all these variables' values and their comparison results. Values look good, nothing looks weird. But when I compare startDate and Now() , how come startDate acts like its greaather than Now() I can't get it.

I have tried :

     If FormatDateTime(startDate) < FormatDateTime(Now()) Then do something
     If FormatDateTime(CDate(startDate)) < FormatDateTime(CDate(Now())) Then do something
     If DateValue(startDate) < DateValue(Now()) Then do something

Also tried nested paranthesis of some of the above . BUT NO LUCK.

None of them worked at all.

This issue just nibbles my brain last couple hours.

Hope its clearly explained.

Looking for suggestions,

Thanks in advance!

EDIT: NOW() is 2013-09-05 12:30 PM

Upvotes: 3

Views: 8535

Answers (3)

user3203955
user3203955

Reputation:

You shoould try casting with CDate()

CDate recognizes date literals and time literals as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight.

You can also find something from here.

hope it helps

Upvotes: 1

curiousBoy
curiousBoy

Reputation: 6834

I would like to answer my own question to help someone who will have same problem.

When I compare it

If FormatDateTime(CDate(startDate)) > FormatDateTime(CDate(Now())) Then 
   Do something...
End If

It has not worked correctly (which is extremely strange, I still didn't get it!)

However , when I remove, FormatDateTime , like this:

If CDate(startDate) > CDate(Now()) Then 
       Do something...
    End If

It returns the correct value.

I don't know what has been changed but just can say 'Went through as expected' .

Thanks

Upvotes: 2

Nicholas Post
Nicholas Post

Reputation: 1857

Try using the DateDiff() function. This will return the difference in whatever interval you want (seconds, minutes, days, etc.). You can then easily compare that to the value you are checking for.

Link: http://msdn.microsoft.com/en-us/library/b5xbyt6f(v=vs.90).aspx

You could also try using the CDate() function to see if converting it to a date will perform properly.

If CDate(startDate) < Now() and CDate(endDate) > Now() Then 
    do something....
End If

Upvotes: 6

Related Questions