• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

add 4 hours to a variable

Explorer ,
Mar 06, 2008 Mar 06, 2008

Copy link to clipboard

Copied

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!
TOPICS
Advanced techniques

Views

335

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Mar 06, 2008 Mar 06, 2008
This works:

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

Thanks for your earlier reply.

Votes

Translate

Translate
LEGEND ,
Mar 06, 2008 Mar 06, 2008

Copy link to clipboard

Copied

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".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 06, 2008 Mar 06, 2008

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 06, 2008 Mar 06, 2008

Copy link to clipboard

Copied

LATEST
This works:

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

Thanks for your earlier reply.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 06, 2008 Mar 06, 2008

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation