# Calculating months between two dates to then populate multiple text fields.

New Here ,
Jun 13, 2019 Jun 13, 2019

Copied

Hi all! I need help!

Ideally I want to be able to calculate the months between DOB and IFSPDate, into months (rounding down if between months (i.e. rounding to 11 if 11.5). I then want the selection of months to populate three different fields with a paragraph of information.

For example:

DOB: 12/01/2017

IFSPDate: 06/13/2019

The following fields would then populate:

CAMonths: 18

SocialSkillsCA: Snapshot of 18 month old children in this area: Children are using words more often to make requests, comments, protest/refuse, greet and respond to others while maintaining eye contact as they interact socially. Children are now often found playing near each other with similar toys. They may bite, hit, scratch or pull hair if overwhelmed and unable to express this frustration with words. Pushing limits, saying ‘no’ and tantrums are all part of children’s development at this age. They may express more complex emotions such as fear, sympathy, modesty, guilt and embarrassment through facial expression and body language more so than words. They can now predict that a broken toy will make another child unhappy. They may attempt to change the other child’s affect by offering a suitable toy.

AcquiringCA: Snapshot of 18 month old children in this area: Children’s play is expanding to include simple pretend play. They will experiment with an object to discover its purpose but no longer need to mouth it. Attention and interest in books increases and they will sit and listen to a shortened version of a story. When asked, they can point to pictures in books and can name familiar objects they see in pictures. During typical family routines like meals, playtime, dressing, etc., they will show they know the names of objects from a variety of categories (e.g., foods, clothes, toys, etc.). They say at least 15 different words that are functional and relate to daily routines, most likely the names of people and objects that are important to them.

BehaviorCA: Snapshot of 18 month old children in this area: Children are very active now- running, carrying larger toys while walking and walking up stairs with a hand held. They can climb up onto a chair and sit down. They actively participate in self-care activities- unzipping a zipper, working on undressing and feeding themselves with a spoon. At meals, they are eating the same foods as other family members. They are more cooperative with teeth brushing and more interested in household activities (imitating some simple activities likes cleaning up spills). Wants and needs are expressed to others with words and gestures during familiar daily routines.

If this is not possible, I at least want to be able to populate three text fields, with a drop down option of CAmonths. I have been successful using the custom vaildation script if 1 month or 2 months is selected, but after the text fields are blank but I am not getting an error in the script. PLEASE HELP!!!

TOPICS
Acrobat SDK and JavaScript, Windows

Views

459

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
19 Replies 19
Jun 14, 2019 Jun 14, 2019

Copied

All the text is unnecessary. You only need to post that you need the difference in Months, and that will be used to put some text in a field.

So there are two issue here.

1) Months calculation

2) conditional for selecting text.

For issue #1 you'll find other similar posts here:

You'll also find many posts here that deal with using "if" statements to select output.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 14, 2019 Jun 14, 2019

Copied

Thank you! I am now able to calculate the months, but still need to round down to the nearest number, I can't seem to find a code for that. Any idea?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 14, 2019 Jun 14, 2019

Copied

Use Math.floor() for that.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 14, 2019 Jun 14, 2019

Copied

This is my current script that is working, but where do I add in something about rounding down?

// Custom calculate script

(function () {

var sStart = getField("IFSP Date").valueAsString;

var sEnd = getField("DOB").valueAsString;

var dStart, dEnd, diff;

if(sStart && sEnd) {

dStart = util.scand("mm/dd/yyyy", sStart);

dEnd = util.scand("mm/dd/yyyy", sEnd);

diff = dEnd.getTime() - dStart.getTime();

event.value = Math.floor(diff / 864e5)/30;

} else {

event.value = "";

}

})();

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 14, 2019 Jun 14, 2019

Copied

Change this:

event.value = Math.floor(diff / 864e5)/30;

To:

event.value = Math.floor(Math.floor(diff / 864e5)/30);

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 14, 2019 Jun 14, 2019

Copied

YOU ARE THE BEST!!!!!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 14, 2019 Jun 14, 2019

Copied

Now that I have a text field that is caluclating from the dates. How can I get the other text fields to populate based on the outcome? I.e. If 1 is selected, I wanted three text fields to populate with the snapshot information. I am not following the conditional execution on how to make this work. Thanks!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 14, 2019 Jun 14, 2019

Copied

At the end of your code you can add something like this:

if (event.value=="1") {

this.getField("SocialSkillsCA").value = "...";

this.getField("AcquiringCA").value = "...";

this.getField("BehaviorCA").value = "...";

} else if (event.value=="2") {

this.getField("SocialSkillsCA").value = "...";

this.getField("AcquiringCA").value = "...";

this.getField("BehaviorCA").value = "...";

}

etc.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 14, 2019 Jun 14, 2019

Copied

Thank you! I was able to get another formula to work, but I appreciate your help! I have one more formula I want to enter. I want to populate a text field with 75% of the Adjusted Age, or of 75% of the Choronological Age if Adjusted Age is not entered, is that possible?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 14, 2019 Jun 14, 2019

Copied

Sure, you can use something like this:

var chronAge = this.getField("Choronological Age").valueAsString;

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 14, 2019 Jun 14, 2019

Copied

YOU ARE THE VERY  BEST!!!!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 17, 2019 Jun 17, 2019

Copied

Try, I need your help one more time, I have a custom calculation script that is working, but I want it to be blank if Due Date is blank. How do I add that in?

// Custom calculate script

(function () {

var sStart = getField("DOB").valueAsString;

var sEnd = getField("DueDate").valueAsString;

var dStart, dEnd, diff;

if(sStart && sEnd) {

dStart = util.scand("mm/dd/yyyy", sStart);

dEnd = util.scand("mm/dd/yyyy", sEnd);

diff = dEnd.getTime() - dStart.getTime();

event.value = Math.floor(Math.floor(diff / 864e5)/30);

} else {

event.value = "";

}

})();

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 17, 2019 Jun 17, 2019

Copied

Use this:

`(function () {var sStart = getField("DOB").valueAsString;var sEnd = getField("DueDate").valueAsString;var dStart, dEnd, diff;if(sStart != "" && sEnd != "") {dStart = util.scand("mm/dd/yyyy", sStart);dEnd = util.scand("mm/dd/yyyy", sEnd);diff = dEnd.getTime() - dStart.getTime();event.value = Math.floor(Math.floor(diff / 864e5)/30);} else {event.value = "";}})();`

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 17, 2019 Jun 17, 2019

Copied

Thank you! I got that to work, but my last calculation doesn't seem to be working. I need a calculation in my Adjusted Age field, that will take the chronological age and subtract the prematurity difference, but I need the field to remain blank if prematurity difference is blank. I have started with this code, but it's not working.

if (PrematurityDifference=="") event.value = "";

Right not if chronological age is 18, and prematurity difference is blank, the adjusted age field shows as 18. Any idea? I am searching the forums too in case this has been addressed somewhere else, but the other scripts I have tried aren't working either.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 17, 2019 Jun 17, 2019

Copied

Assuming the other fields are calculated, check the fields calculation order (NOT the same as the tab order).

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 17, 2019 Jun 17, 2019

Copied

All seems to be in the right order, adjusted age is calculated after the prematurity difference, but it still is showing up with a number. I need it to be blank if prematurity is not calculated.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 17, 2019 Jun 17, 2019

Copied

I think we'll need to see the actual file to be able to help you further. You can share it by uploading it to a file-sharing website like Dropbox, Google Drive, Adobe Document Cloud, etc., and then post the link to it here.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 18, 2019 Jun 18, 2019

Copied

LATEST

Thank you, I actually figured it out. You have been so helpful!! One last question, my text fields that autopopulate based on other fields, is there a way a user can delete the information in the text field if it is not needed, and even type in something else? This should be my last step!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 17, 2019 Jun 17, 2019