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

Calculate 10 years from a date field

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

TOPICS
Acrobat SDK and JavaScript , Windows

Views

1.4K

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

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();

//

...

Votes

Translate

Translate
Community Expert ,
Jul 13, 2018 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

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

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

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

Copy link to clipboard

Copied

Thank you so much try67. I really appreciate this.

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

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 ,
Jul 13, 2018 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++;

}

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

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

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

Copy link to clipboard

Copied

Do you mean when the original field is empty?

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

Copy link to clipboard

Copied

Correct.

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

Copy link to clipboard

Copied

LATEST

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);

}

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