4 Replies Latest reply on Mar 6, 2008 1:07 PM by Newsgroup_User

    add 4 hours to a variable

    JoyRose
      I need to add 4 hours to a variable. This is the variable:
      <cfparam name="application.startFacTime" default = "#Dateformat(now())#" />

      I want to compare application.startFacTime + 4 hours to Now.

      If Now and the variable are the same date, I want to see if adding 4 hours to the
      variable will result a time that's 4 or more hours older than Now.

      If that's true, I run a query and do some other stuff and then
      reset the variable to Dateformat(now())

      Thanks for any help!
        • 1. Re: add 4 hours to a variable
          Dan Bracuk Level 5
          Step number 1 - delete the dateformat function in your cfparam tag. It returns a string and you want a datetime object.

          Step number 2 - create a new variable with by adding 4 hours to the application variable.

          Step 3 - see if the day portions of your application variable and new variables are the same.

          If you don't know how to do steps 2 and 3, google "coldfusion date functions".
          • 2. Re: add 4 hours to a variable
            JoyRose Level 1
            Can you show me what the code would look like: "create a new variable with by adding 4 hours to the application variable"


            I can't seem to get any code to work that adds 4 hours to the variable.

            For instance:
            1.)<CFIF....
            IsDefined("application.startFacTime") AND (
            DateCompare(DateFormat(application.startFacTime),DateFormat(now())) is 0 AND
            DateCompare(Hour(application.startFacTimeMin),Hour(now())) is -1>

            This works to compare the variable to Now, but that doesn't do any good.

            I need something like this pseudo-code:

            DateCompare(Hour(application.startFacTimeMin)+4 hours,Hour(now())) is -1



            2.) This "works" but, like above, it doesn't do me any good:
            <CFIF....
            DateCompare(application.startFacTime,DateFormat(now())) is 0 AND
            #Abs(DateDiff('h', application.startFacTime, Dateformat(now())))# lt 1
            </cfif>

            I need something like this pseudo-code:
            #Abs(DateDiff('h', application.startFacTime + 4 hours, Dateformat(now())))# lt 1

            Thanks for your help.
            • 3. Re: add 4 hours to a variable
              Level 7
              To repeat several posts, DO NOT USE dateFormat(). 'dateFormat' is for
              formating a date as a string for display. You don't want to use a
              string for mathematical manipulation. Leave your dates as date objects
              for manipulation then format the final result as a string for display.

              Here is a step by step example:

              <cfset now = now()>
              <cfset then = parseDateTime("3/5/2008 1:15")>

              <cfoutput>
              #dateFormat(now,"dd/mm/yyyy")# #timeFormat(now,"hh:mm:ss")#<br />
              #dateFormat(then,"dd/mm/yyyy")# #timeFormat(then,"hh:mm:ss")#<br />
              #dateFormat(dateAdd("h",4,now),"dd/mm/yyyy")#
              #timeFormat(dateAdd("h",4,now),"hh:mm:ss")#<br />
              #dateCompare(then,dateAdd("h",4,now))#
              </cfoutput>
              • 4. Re: add 4 hours to a variable
                JoyRose Level 1
                This works:

                <cfparam name="application.startFacTime" default = "#dateAdd('h',4, now())#" />

                Thanks for your earlier reply.