Calculate 10 years from a date field

Community Beginner ,
Jul 13, 2018

Copy link to clipboard

Copied

Hi,
I need help creating a JavaScript in Acrobat to calculating 10 years from a Contract Date field. I'm having difficulty with getting this to come out correctly because i need to account for leap years. Any help would be greatly appreciated.

Thank you!

Adobe Community Professional
Correct answer by BarlaeDC | Adobe Community Professional

HI,

I am assuming you are using a date picker but it is no real problem if you are not, if you use the code below then the Date object will cope with all the leap years and such.

// this is where I get the date from ( a standard date picker)

var myDate = this.getField("Date3_af_date").valueAsString;

// I make the string an actual date

var myDateValue = new Date ( myDate);

// get the years and add 10

myDateYears = myDateValue.getFullYear() + 10;

// get the months

myDateMonths = myDateValue.getMonth();

// get the days

myDateDays = myDateValue.getDate();

// using the values above, create a new date, this should automatically deal with leap years and such

var myDate = new Date ( myDateYears, myDateMonths, myDateDays);

// put the returned value into a text field to see.

this.getField("Text4").value = myDate.toString();

Hope this helps

Malcolm

TOPICS
Acrobat SDK and JavaScript, Windows

Views

774

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

Calculate 10 years from a date field

Community Beginner ,
Jul 13, 2018

Copy link to clipboard

Copied

Hi,
I need help creating a JavaScript in Acrobat to calculating 10 years from a Contract Date field. I'm having difficulty with getting this to come out correctly because i need to account for leap years. Any help would be greatly appreciated.

Thank you!

Adobe Community Professional
Correct answer by BarlaeDC | Adobe Community Professional

HI,

I am assuming you are using a date picker but it is no real problem if you are not, if you use the code below then the Date object will cope with all the leap years and such.

// this is where I get the date from ( a standard date picker)

var myDate = this.getField("Date3_af_date").valueAsString;

// I make the string an actual date

var myDateValue = new Date ( myDate);

// get the years and add 10

myDateYears = myDateValue.getFullYear() + 10;

// get the months

myDateMonths = myDateValue.getMonth();

// get the days

myDateDays = myDateValue.getDate();

// using the values above, create a new date, this should automatically deal with leap years and such

var myDate = new Date ( myDateYears, myDateMonths, myDateDays);

// put the returned value into a text field to see.

this.getField("Text4").value = myDate.toString();

Hope this helps

Malcolm

TOPICS
Acrobat SDK and JavaScript, Windows

Views

775

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
Jul 13, 2018 0
Adobe Community Professional ,
Jul 13, 2018

Copy link to clipboard

Copied

HI,

I am assuming you are using a date picker but it is no real problem if you are not, if you use the code below then the Date object will cope with all the leap years and such.

// this is where I get the date from ( a standard date picker)

var myDate = this.getField("Date3_af_date").valueAsString;

// I make the string an actual date

var myDateValue = new Date ( myDate);

// get the years and add 10

myDateYears = myDateValue.getFullYear() + 10;

// get the months

myDateMonths = myDateValue.getMonth();

// get the days

myDateDays = myDateValue.getDate();

// using the values above, create a new date, this should automatically deal with leap years and such

var myDate = new Date ( myDateYears, myDateMonths, myDateDays);

// put the returned value into a text field to see.

this.getField("Text4").value = myDate.toString();

Hope this helps

Malcolm

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...
Jul 13, 2018 2
Community Beginner ,
Jul 13, 2018

Copy link to clipboard

Copied

Thank so much Malcolm! How do I display this as mm/dd/yyyy in the script because if I try to use the Format tab it doesn't give me the correct 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...
Jul 13, 2018 0
Most Valuable Participant ,
Jul 13, 2018

Copy link to clipboard

Copied

In the last line change this:

myDate.toString();

To:

util.printd("mm/dd/yyyy", myDate);

Also, if this is a calculation script, change the first part of that line to:

event.value = ...

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...
Jul 13, 2018 2
Community Beginner ,
Jul 13, 2018

Copy link to clipboard

Copied

Thank you so much try67. I really appreciate this.

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...
Jul 13, 2018 0
Community Beginner ,
Jul 13, 2018

Copy link to clipboard

Copied

Sorry but my end user now wants 1 month added to the above calculation.

So if my Contract Date is 07/13/2018, the new script above adds 10 years 07/13/2028, adding 1 month should return a result of 08/13/2028.

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...
Jul 13, 2018 0
Most Valuable Participant ,
Jul 13, 2018

Copy link to clipboard

Copied

Change this line:

myDateMonths = myDateValue.getMonth();

To:

myDateMonths = myDateValue.getMonth()+1;

if (myDateMonths==12) {

     myDateMonths = 0;

     myDateYears++;

}

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...
Jul 13, 2018 2
Community Beginner ,
Jul 13, 2018

Copy link to clipboard

Copied

try67 You are the best! Thanks for making me look good. I feel like I should send you chocolate.

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...
Jul 13, 2018 0
Community Beginner ,
Jul 13, 2018

Copy link to clipboard

Copied

Now how do I get the default 01/00/0000 not to appear in the fields? Is there a validation script I can run?

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...
Jul 13, 2018 0
Most Valuable Participant ,
Jul 13, 2018

Copy link to clipboard

Copied

Do you mean when the original field is 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...
Jul 13, 2018 0
Community Beginner ,
Jul 13, 2018

Copy link to clipboard

Copied

Correct.

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...
Jul 13, 2018 0
try67 LATEST
Most Valuable Participant ,
Jul 13, 2018

Copy link to clipboard

Copied

Since the code has been modified quite a bit I'm posting it in full again, in its latest form:

var myDate = this.getField("Date3_af_date").valueAsString;

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

else {

    var myDateValue = new Date(myDate);

    var myDateYears = myDateValue.getFullYear() + 10;

    var myDateMonths = myDateValue.getMonth()+1;

    if (myDateMonths==12) {

        myDateMonths = 0;

        myDateYears++;

    }

    var myDateDays = myDateValue.getDate();

    var myDate = new Date ( myDateYears, myDateMonths, myDateDays);

    event.value = util.printd("mm/dd/yyyy", myDate);

}

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...
Jul 13, 2018 0