Skip to main content
rachelle_b
Known Participant
July 13, 2018
Answered

Calculate 10 years from a date field

  • July 13, 2018
  • 2 replies
  • 2567 views

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!

This topic has been closed for replies.
Correct answer BarlaeDC

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

2 replies

rachelle_b
Known Participant
July 13, 2018

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.

try67
Community Expert
Community Expert
July 13, 2018

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 = ...

rachelle_b
Known Participant
July 13, 2018

Thank you so much try67. I really appreciate this.

BarlaeDC
Community Expert
BarlaeDCCommunity ExpertCorrect answer
Community Expert
July 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();

// 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