Skip to main content
björnh57454914
Known Participant
February 5, 2018
Answered

JS-Code for 3/6 Month to add to a date (date of employment)

  • February 5, 2018
  • 2 replies
  • 3867 views

Hey there, I need help with a JS-Code.

I have two fields in my PDF-Formular one (date of employment) which is edited dd.mm.yyy

                                                            one Dropdown with 3 or 6 (months)

Now I like to calculate means to add to the date of employment.

Can somone send me the right Code?

THX

Björn

This topic has been closed for replies.
Correct answer try67

Hey gk,

thx a lot for your plan.

My experience in JS are ZERO...! :-)

I know that I have to bild it up like Basic but I realy need a code because then i can see what happens und how it will build...

THX

BJ


OK, I will help you get there... Let's say you have 3 fields. One for the starting date (called "StartDate"), one for the length of employment (called "Months") and one for the final date of employment (called "EndDate"). You can use this custom calculation script for the latter:

var startDateString = this.getField("StartDate").valueAsString;

var months = Number(this.getField("Months").valueAsString);

if (startDateString=="" || months==0) event.value = "";

else {   

    var d = util.scand("dd.mm.yyyy", startDateString);

    d.setMonth(d.getMonth()+months);

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

}

2 replies

Inspiring
February 5, 2018

Thom Parker has written some tutorials on this:

https://acrobatusers.com/tutorials/date_time_part1

Working with date and time in Acrobat JavaScript (Part 1 of 3)

Working with date and time in Acrobat JavaScript (Part 2 of 3)

Working with date and time in Acrobat JavaScript (Part 3 of 3)

Joel Geraci has written The practicalPDF Date Library for Adobe Acrobat

JavaScript also supports the getMonth() and setMonth() methods for modifying the JavaScript date object.

"Month" is a rather vague term since the number of days in a month can be 28, 29, 30, or 31 depending upon the month and year. Some banker calculations for interest assume a uniform 30 day month.

try67
Community Expert
Community Expert
February 5, 2018

3/6 months to the day? Does it always start on a specific day? What happens if the last day of the first month doesn't exist in the last one?

björnh57454914
Known Participant
February 5, 2018

THX

I just want to add to months and hope the days will be korrekt... ;-)

Inspiring
February 5, 2018

One first needs to convert the data string to the date object using the "util.scand()" method in Acrobat JavaScript and then uses the JavaScript getMonth() method to get the zero based month of the year and adds 3 or 6 to that value. The new value is used in the setMonth() method to update the date object. Now one uses the util.;printd() method to format the revised date string.