dateCompare, dateDiff or..?

Since I started using CF years ago, Im struggling with the dateCompare and dateDiff functions. Every time when one of them crosses my path I promise myself to remember the solution for next time. But

As Time Goes By
i forget about them until I need one of the two again, like this morning.

I have two time variables:


<cfset currentTime = CreateTime(Hour(Now()), Minute(Now()), Second(Now()))>
<cfset Session.reviewTime = CreateTime(Hour(Now()), Minute(Now()), Second(Now()))>

The second one, as you probably understand, is created when a visitor submit a form. From the moment Session.reviewTime exist I would like to output a message for about 20 minutes. This is what I tried:


<cfif structKeyExists( Session, 'reviewTime' ) AND dateCompare( currentTime, Session.reviewTime, 'n' ) LTE 20 > 
    <cfoutput>#stringValue#</cfoutput>
</cfif>

And I tried several different functions as well, like dateAdd and dateDiff but unfortunately without success.

To get the current time, you can simply say:

<cfset currentTime = now() />

It will be a full timestamp date/time object, but it will contain the time as well (for where the CF server exists, mind you)

As far as the logic goes, I would say:

<cfif structKeyExists( session, “reviewTime” ) and now() gt dateAdd( “n”, 20, currentTime )>
<!— It is beyond the 20 minute marker.—>
<cfelse>
<!— It is still within the 20 minute marker. (or session.reviewTime doesn’t exist) —>
</cfif>

Thank you for the response. You’re absolutely right about the currentTime variable but I had It like that since I was/and still am struggling with the logic.

Your logic made me think though, but it is still not how it should be. You have:


<cfif structKeyExists( session, "reviewTime" ) and now() gt dateAdd( "n", 20, currentTime )>

<cfelse>

</cfif>

And that confusing me since the session.reviewTime is not coming back in the dateAdd function. While I think it should. :confused:

If session.reviewTime exists and the current time is less than session.reviewTime plus 20 min, show the output

otherwise If session.reviewTime exists and the current time is more than session.reviewTime plus 20 min don’t show the output

But if i replace the currentTime in the dateAdd function with session.reviewTime it isn’t working either.

This is one of those times that I simply don’t see the logic. Please help me to see the light :afro:


<!--- 

    Set this to a time that is either outside of 20 minutes from now
    or inside 20 minutes from now.
    
--->
<cfset datThen = createDateTime( 2010, 12, 4, 12, 15, 0 ) />

<cfif isDefined( "datThen" )>

    <cfif now() lt dateAdd( "n", 20, datThen )>
    
        <p>It has been less than 20 minutes since time set.</p>
        
    <cfelse>
    
        <p>It has been over 20 minutes since time was set.</p>
    
    </cfif>
    
<cfelse>

    <p>Variable 'datThen' does not exist.</p>

</cfif>

I did a quick test and this works flawlessly. When I set the first variable to a value that is 20 minutes from now, it reports properly. When I set it to a time later than that, it reported properly. When I changed the variable so it doesn’t exist, it told me so.

Replace your vars and test with this code.

BTW, I am running:

ColdFusion 9.0.1 on IIS 7.5 on Windows 7 Pro x64

Aaron thank you again for your response. You’re absolutely right. I wonder what I was thinking in the first place :confused: The logic was simply not there when I wrote my first message and the reply to your first message. There is and has been there all the time a timestamp in the reviews table review_date and I simply didn’t think of using that one so now i basically use what you said all along :slight_smile:


<cfset expireTime = dateAdd( 'n', 20, getReviews.review_date )>


<cfif structKeyExists( Session, 'review' ) AND Session.review EQ review_id AND now() LTE expireTime>
..........
<cfelse>

</cfif>

Thank you for all suggestions, they are highly appreciated :tup:

So is it working for you or are you still having a problem? For me, one of the best troubleshooting processes is:

  1. Have a second person take a look at the code (not always possible, but usually nails the issue in a handful of minutes)
  2. Step by step run your code, and cfdump vars and logic to make sure they are coming back as you are expecting them to.

Hey if you want to teach me something, how do you get this forum to see your code samples as Coldfusion code? Can’t seem to figure it out :slight_smile:

Everything is working fime now Aaron, thank you again :slight_smile: About code use in the forum if you post or reply to a post on the second row of the text editor all the wy to the right is a drop down(Select Syntax). There are more than one way of using the tags. You can first place a tag and after that paste your code within the opening and closing tag or you add your code, select the code and then choose the appropriate tag.

Hope this make sense :slight_smile:

<cfset thanks = "It seems I was always using the basic editor, that's why I never saw the option." />
<cfset thanks &= "Glad to see it's working." />

:lol:

Good that you now know.

By the way about what you said about having a second person looking at your code, I like that idea myself as well. So I hope I keep you to that. There are those times, that under a lot of stress and time pressure, you even don’t see the simplest mistakes any longer

Cheers

I hear ya. At work, I have 1 other web developer. And when we get stuck, we give it some time to work it out on our own. But if we can’t move forward, we get the other person in there.

On days when that person was out sick, I could spend freaking hours of wasted time trying to find the issue. Very frustrating.