Highlighted

Javascript help

New Here ,
Sep 21, 2020

Copy link to clipboard

Copied

I have 2 date fields in my form. I am trying to create a third "deadline" field that calculates and displays the deadline which is 90 days from the earlier of the 2 dates. How would I do this using javascript?

TOPICS
Acrobat SDK and JavaScript

Views

126

Likes

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

Javascript help

New Here ,
Sep 21, 2020

Copy link to clipboard

Copied

I have 2 date fields in my form. I am trying to create a third "deadline" field that calculates and displays the deadline which is 90 days from the earlier of the 2 dates. How would I do this using javascript?

TOPICS
Acrobat SDK and JavaScript

Views

127

Likes

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
Sep 21, 2020 0
Adobe Community Professional ,
Sep 21, 2020

Copy link to clipboard

Copied

Basically you can use the script below as a custom calculation script for the "deadline" field:

 

var s = this.getField("myDateField").valueAsString;  

    var d = util.scand("mmmm d, yyyy", s);  
    d.setDate(d.getDate()+90);  
    event.value = util.printd("mmmm d, yyyy", d);  

 

I am not sure if you you want to run a validation script or similar to determine which of the two datefields has the earliest date. And based on that , then execute the script above. Otherwise the script above should work fine using just one of the date fields as the start date. 

Likes

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
Reply
Loading...
Sep 21, 2020 0
Most Valuable Participant ,
Sep 22, 2020

Copy link to clipboard

Copied

That's only part of what they asked for... The first part is to compare the two dates and determine which is the earlier one, and then use that as the basis for the deadline calculation.

Likes

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
Reply
Loading...
Sep 22, 2020 0
Adobe Community Professional ,
Sep 22, 2020

Copy link to clipboard

Copied

Here's the calculation for the deadline field

 

var oDate1 = util.scand("mmmm d, yyyy", this.getField("Date1")); 
var oDate2 = util.scand("mmmm d, yyyy", this.getField("Date2"));
var oStart = (oDate1<oDate2)?oDate1:oDate2;
oStart.setDate(oStart.getDate()+60);
event.value = util.printd("mmm d, yyyy",oStart);
   

Likes

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
Reply
Loading...
Sep 22, 2020 0
Community Beginner ,
Sep 22, 2020

Copy link to clipboard

Copied

Hi, Thom, I was trying to use this code but whatever I put in first 2 fields it shows only result counting from todays date.

Likes

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
Reply
Loading...
Sep 22, 2020 0
Most Valuable Participant ,
Sep 22, 2020

Copy link to clipboard

Copied

Change the first two lines to:

 

var oDate1 = util.scand("mmmm d, yyyy", this.getField("Date1").valueAsString);
var oDate2 = util.scand("mmmm d, yyyy", this.getField("Date2").valueAsString);

 

However, this will still mean that if a field is empty it will be treated like it's today's date. If you don't want that you should let us know what you do want to happen if either (or both) of those fields are empty.

Likes

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
Reply
Loading...
Sep 22, 2020 0
Community Beginner ,
Sep 22, 2020

Copy link to clipboard

Copied

I thought it works like normal calculation, every time I change date in first 2 fields it will auto update result.

Likes

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
Reply
Loading...
Sep 22, 2020 0
Most Valuable Participant ,
Sep 22, 2020

Copy link to clipboard

Copied

It will do that, yes.

Likes

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
Reply
Loading...
Sep 22, 2020 1
Adobe Community Professional ,
Sep 22, 2020

Copy link to clipboard

Copied

Good Catch, thanks!!

 

Likes

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
Reply
Loading...
Sep 22, 2020 0