Classic Asp date comparison not working correctly

可紊 提交于 2019-12-23 16:33:52

问题


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


回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/18644293/classic-asp-date-comparison-not-working-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!