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

Switch from printing value to making it a variable

Community Beginner ,
May 08, 2022 May 08, 2022

Copy link to clipboard

Copied

I have a JS which puts the "15" in a form field from another form field called "Start Date" : 12/15/2022.

If the "Start Date" has 03/05/2022, it shows: 05

The Script is:

var SD = this.getField("Start Date").valueAsString;
{var d = util.scand("mm.dd.yyyy", SD);d.setMonth(d.getMonth()); d.setDate(d.getDate());
event.value = util.printd("dd", d);}

and this works fine.

But I need to use this data in another form field js so I need it to be a variable instead of "printing" it in the field.

Something like:

var SDD =

(var SD = this.getField("Start Date").valueAsString;
{var d = util.scand("mm.dd.yyyy", SD);d.setMonth(d.getMonth()); d.setDate(d.getDate());
event.value = util.printd("dd", d);})

I have tried several variations with no luck.

 

Thanks!!

 

TOPICS
JavaScript , PDF forms

Views

1.9K

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

Community Expert , May 09, 2022 May 09, 2022

If you use mm/dd/yyyy date format put this script in "Start Date" field as 'Validation' script and then just call SDD in other scripts as needed:

var SD = event.value;
var str = SD.split("/");
var SDD = str[1];

Votes

Translate

Translate
Community Expert ,
May 08, 2022 May 08, 2022

Copy link to clipboard

Copied

You don't need a variable. Use the value of this field.

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 Beginner ,
May 08, 2022 May 08, 2022

Copy link to clipboard

Copied

True, but i am deleting that form field, but still need the value of that script.

Meaning: I need to use the value from that SDD Field and multiply it by another field value. but i will not have the form field "SDD" tha returns the actual number to referance to. so i need to run that code and make it a variable to use in another calculation all in one script. I need the results of the SDD to be a variable i can referance to without referancing to an actual form field value.

 

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 08, 2022 May 08, 2022

Copy link to clipboard

Copied

Where does you use the script when you delete the form field?

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 Beginner ,
May 09, 2022 May 09, 2022

Copy link to clipboard

Copied

Its completely deleted. I made the form to practice with. The form will not be in the final doc.

Thats why i am trying to "combine" the script into another one and need to be able to referance it with out using the form field. Let me work on getting an example to you.

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 09, 2022 May 09, 2022

Copy link to clipboard

Copied

I have a JS which puts the "15" in a form field from another form field called "Start Date" : 12/15/2022.

…

{var d = util.scand("mm.dd.yyyy", SD);d.setMonth(d.getMonth()); d.setDate(d.getDate());

 

I guess a format issue, the script uses dot as separators in the dates and you fill the fields with slash as separators.

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 09, 2022 May 09, 2022

Copy link to clipboard

Copied

Not an error, but these lines of code are tautological and have no effect on the outcome. They can be removed:

 

d.setMonth(d.getMonth()); d.setDate(d.getDate());

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 09, 2022 May 09, 2022

Copy link to clipboard

Copied

If you use mm/dd/yyyy date format put this script in "Start Date" field as 'Validation' script and then just call SDD in other scripts as needed:

var SD = event.value;
var str = SD.split("/");
var SDD = str[1];

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 Beginner ,
May 10, 2022 May 10, 2022

Copy link to clipboard

Copied

Thank you Nesa!

That is exactly what I was trying to accomplish.

I would like to do the same with another calculation script.

I would like to put this in a validation script so I can call it when needed...

This script finds the number of days in a month. It works just fine in its own form field, but again, I will be deleting this field.

I have tried putting it just like it is written in the "Start Date" field as a validation but it doesnt wok

 

var SD = this.getField("StartDate").valueAsString;
{var d = util.scand("mm.dd.yyyy", SD);d.setMonth(d.getMonth()); d.setDate(d.getDate());
var DIM = daysInMonth(d);
event.value = DIM;}

 

Not sure why this doesnt work like that. It already has a variable DIM but it only works in is own form field.

I need to call this variable DIM from other scripts.

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 10, 2022 May 10, 2022

Copy link to clipboard

Copied

To get number of days in a month use this:

function numofdays (month, year) {
return new Date(year, month, 0).getDate();}
var SD = event.value;
var str = SD.split(".");
var DIM = numofdays(str[0],str[2]);

 

This is for format "mm.dd.yyyy".

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 Beginner ,
May 10, 2022 May 10, 2022

Copy link to clipboard

Copied

Thank you again Nesa!

I will try that and see how it works.

Can I put this in the same "Start Date" field as 'Validation' in addition with the other script you gave me for SDD?

 

Ultimately what I am trying to do is calculate Prorate amounts for rent. The only fields I have in the document are: "Start Date", "Rent", "Prorate 1", and "Prorate 2". So I need to determine how many days are in the month which is "DIM", and that is used to determine a 'daily rate' (Rent/DIM). And then I need to know the day of the start date which is "SDD" and then subtract the differance (DIM-SDD) to multiply times the daily rate to get "Prorate 1". So, I am trying to make variables for SDD, Rent, DIM, Daily Rate, which can be referance by other form fields. The SDD you gave me looks to be working. I used your SDD script and modified it for "Rent" and put it in the "Rent" form field 'validation':

var RNT = event.value;

And that appears to be working as well.

Let me make a sample doc with the fields that will be available to use and I will get it uploaded.

I will try to get this tomorrow. Thank you!!

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 Beginner ,
May 12, 2022 May 12, 2022

Copy link to clipboard

Copied

Ok, here is a sample sheet. I hope I have explained what I am trying to do.

Thank you!!

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 12, 2022 May 12, 2022

Copy link to clipboard

Copied

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 Beginner ,
May 14, 2022 May 14, 2022

Copy link to clipboard

Copied

Thank you Nesa for the work. But it is not working I don't think. When I change the date, nothing happens.

When I change the Rent amount, nothing happens. I made a test field with event.value = SDD, and it returns nothing or blank. I tried it with the other variables, DIM, DR, SD  and nothing is calculated.

What you wrote looks like it should work flawlessly.... not sure what the problem is.

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 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Thats because variables are in validation so until you change both fields variables will be undefined, I moved both scripts to calculation instead, try now:

https://drive.google.com/uc?export=download&id=1Rptn6CXY2U4_zOsuQ0x01FRBFK16I5JI 

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 Beginner ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Oh, Okay. I did not know that.

Thank you!!

I will give it a try tomorrow when I get back to my office.

Thank you again!!

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 Beginner ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

LATEST

Thank you very much Nesa!! This seems to be doing exactly what I needed it to do.

I noticed that you have to enter the full date in "Start Date" in order for it to work like 04/15/2022.

Before I could just enter 04/15 or 4/15 and it would automaticlly display 04/15/2022, but thats not a big deal.

I just liked it that way because it kept people from entering the wrong year. Some people forget what year we are in... LOL

Thank you again Nesa!! 

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