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

Pass variable in Dateadd() function

Explorer ,
May 17, 2013 May 17, 2013

Copy link to clipboard

Copied

Hi,

I want pass a variable for number part in Dateadd() funtion instead of hardcoding it.

Eg:

<cfset var a = 180>

now i want to pass this value of a into "number" part of dateadd function.

DateAdd("d", number, now())

How can be this done..?


Views

1.5K

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
Community Expert ,
May 18, 2013 May 18, 2013

Copy link to clipboard

Copied

<cfset var a = 180>

<cfset newDate = DateAdd("d", a, now())>

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 ,
May 19, 2013 May 19, 2013

Copy link to clipboard

Copied

i tried that.but getting error

2.png

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
Community Expert ,
May 19, 2013 May 19, 2013

Copy link to clipboard

Copied

gokul1242 wrote:

i tried that....

No, you didn't. What you tried is equivalent to:

<cfset txt_expiration = "">

<cfset yearAhead = DateAdd("d", txt_expiration, now())>

You will find that the following does work:

<cfset txt_expiration = 180>

<cfset yearAhead = DateAdd("d", txt_expiration, now())>

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 ,
May 19, 2013 May 19, 2013

Copy link to clipboard

Copied

Yes

<cfset txt_expiration = 180>

<cfset yearAhead = DateAdd("d", txt_expiration, now())>

This works. But in my case i am calling txt_expiration value from a sql query through js script as follows

<SCRIPT>

        <cfwddx action="cfml2js" input="#qry_category#" topLevelVariable="qj">

       

        function fnselectcat()

        {

           

                if(qj.getField(i,"cat_id") == document.frm.sel_category.value)

                    {

                       

                        document.frm.txt_expiration.value = qj.getField(i,"CAT_EXPIRATION");

                    }

                   

           

        }

    </SCRIPT>

how to fetch the "txt_expiration" value from script and pass it into the date funtion ??

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
Community Expert ,
May 20, 2013 May 20, 2013

Copy link to clipboard

Copied

Javascript runs on the browser, ColdFusion on the server. Hence, Javascript variables are not available to ColdFusion.

You are apparently aware of one way of passing the variable to ColdFusion, namely, by submitting a form. The variable is then available on the form's action page as form.txt_expiration.

However, I was wondering why don't just do the obvious. Get the value of txt_expiration directly from the query!

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 ,
May 20, 2013 May 20, 2013

Copy link to clipboard

Copied

the scenario is i have a drop down.based on selecting the drop down value it calls the above onchange js function to display the corresponding

txt_expiration  value .but txt_expiration values are in days(eg: 180,365) in db..i need to add it to current date ie now() and  convert it and display in dd-mm-yyyy format

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
Community Expert ,
May 20, 2013 May 20, 2013

Copy link to clipboard

Copied

LATEST

Run the following test. I hope everything will then become clear. Both CFM pages are within the same directory.

testFormpage.cfm

<SCRIPT>

  function fnselectcat()

  {     document.frm.txt_expiration.value = 'KARAMBA!';

        document.frm.submit();

  }

</SCRIPT>

   

<body onload="fnselectcat()" >

<form name="frm" method="post" action="testActionpage.cfm">

    <input name="txt_expiration" type="text">

</form>

</body>

testActionpage.cfm

<cfif isdefined("form.txt_expiration")>

   The form has been submitted and the value of  form.txt_expiration is: <cfoutput>#form.txt_expiration#</cfoutput>

</cfif>

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