Skip to main content
Participant
November 20, 2008
Question

Need help with DateFormat fromCF4.0 to CF8

  • November 20, 2008
  • 5 replies
  • 577 views
I'm migrating from CF4.0 to CF8 and getting stuck with the following error message:

20-Nov-08 11:32:53 is an invalid date or time string.

The error occurred in *:********\*******\login.cfm: line 171

169 : <CFELSE>
170 :
171 : <CFIF ParameterExists(REMOTE_ADDR) AND #CreateODBCDate("#GetUserRecord.lastlogin#")# LT #CreateODBCDate("#SetDateTimeNow#")#>
172 : <CFQUERY NAME="UpdateDateTimeIPLast" DATASOURCE=#Attributes.DATASOURCE#>
173 : UPDATE #Attributes.TABLE#

The chunck of code that I think is involved is:

<CFSET SetDateTimeNow = "#DateFormat(DateAdd('n', 0, NOW()))#
#TimeFormat(DateAdd('n', 0, NOW()), 'HH:mm:ss')#">

I could really use some help here. MANY THANKS.

    This topic has been closed for replies.

    5 replies

    BKBK
    Community Expert
    Community Expert
    November 24, 2008
    -==cfSearching==- wrote:
    The name should be in quotes with no # signs around the variable

    Duly corrected. Thanks.

    Inspiring
    November 23, 2008
    > <!--- isDefined(cgi_param) is always true for most, if not all, cgi
    > parameters, and so may be omitted --->

    It's true for all of them. Even isDefined("CGI.not_a_cgi_variable") will
    return true (or, knowing CF, probably "yes", but it amounts to the same).
    This is stupid, but it's the way CF works.

    I would recommend using structKeyExists() over isDefined(). One can write
    sloppy code with isDefined() - as per this case: not scoping the variable.
    isDefined() also has a history (not sure about CF8, but definitely CFMX7)
    of returning false positives with session-scoped variables.

    --
    Adam
    BKBK
    Community Expert
    Community Expert
    November 23, 2008
    A faithful translation of your code snippet into CF8 is as follows:

    <CFSET aDate = DateAdd('n', 0, NOW())>
    <CFSET SetDateTimeNow = parseDateTime(DateFormat(aDate) & ' ' & TimeFormat((aDate), 'HH:mm:ss'))>
    <!--- parameterExists() is deprecated; replace with isDefined() --->
    <!--- isDefined("cgi_param") is always true for most, if not all, cgi parameters, and so may be omitted --->
    <CFIF CreateODBCDate(GetUserRecord.lastlogin) LT CreateODBCDate(SetDateTimeNow)>


    P.S.: I said faithful. Still, I'm more inclined to Dan Bracuk's use of now().




    Inspiring
    November 23, 2008
    > <!--- parameterExists() is deprecated; replace with isDefined() --->
    > <!--- isDefined(cgi_param) is always true for most, if not all, cgi parameters, and so may be omitted --->

    .. Also a general word about switching from ParameterExists to IsDefined. IIRC IsDefined works differently than Parameter exists. With IsDefined, pass in the variable name as a string (ie The name should be in quotes with no # signs around the variable)

    <!--- correct --->
    <cfif isDefined("form.firstName")>
    found
    </cfif>

    <!--- Incorrect. It will either cause an error or return the wrong result --->
    <cfif isDefined(form.firstName)>
    found
    </cfif>
    Inspiring
    November 21, 2008
    To solve your syntax problem, change this
    <CFIF ParameterExists(REMOTE_ADDR) AND #CreateODBCDate("#GetUserRecord.lastlogin#")# LT #CreateODBCDate("#SetDateTimeNow#")#>

    to this
    <CFIF ParameterExists(REMOTE_ADDR) AND GetUserRecord.lastlogin LT now())>

    From a logic perspective, will getuserrecord.lastlogin ever be greater than or equal to than the current date and time?
    Inspiring
    November 20, 2008
    Brings back unfond memories of our upgrade from 4.5 to 7. The gist of it is that you can't get away with improper (even though it worked before) coding as much in higher versions.

    In your case, you are trying to perform date functions on strings. Dateformat returns a string.

    The quickest fix is to replace the SetDateTimeNow variable with the now() function.

    Then you can ask yourself if it's actually possible for the lastlogin to be in the future.
    tdolAuthor
    Participant
    November 21, 2008
    Thanks Dan.
    If it's not too much trouble would you mind showing me exactly what the original code needs to be changed to? I'm real rusty on this stuff. Thanks.