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

Explorer ,
Feb 05, 2018 Feb 05, 2018

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript, Windows

Views

536

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
community guidelines

correct answers 1 Correct Answer

Most Valuable Participant , Feb 06, 2018 Feb 06, 2018
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.yyy...

Likes

Translate

Translate
Most Valuable Participant ,
Feb 05, 2018 Feb 05, 2018

Copy link to clipboard

Copied

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?

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
community guidelines
Explorer ,
Feb 05, 2018 Feb 05, 2018

Copy link to clipboard

Copied

THX

I just want to add to months and hope the days will be korrekt... 😉

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
community guidelines
LEGEND ,
Feb 05, 2018 Feb 05, 2018

Copy link to clipboard

Copied

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.

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
community guidelines
Explorer ,
Feb 05, 2018 Feb 05, 2018

Copy link to clipboard

Copied

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

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
community guidelines
Most Valuable Participant ,
Feb 06, 2018 Feb 06, 2018

Copy link to clipboard

Copied

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

}

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
community guidelines
Explorer ,
Feb 06, 2018 Feb 06, 2018

Copy link to clipboard

Copied

Hi Try,

it runs perfect! THX!!!

I've changed your two fieldnames in my Names and it works!

Is it possible to subtract one day from the hole calculation, now?

So I have the correct timeline of my employees qualifying period.

For instance: "StartDate" = 01.01.2018

                       "Months"   = 6

                       "EndDate" = 01.07.2018            ( - 1 day)

qualifying Period ends:    = 30.06.2018

With your JS I can follow the Logical sequence to try other equal JS for later...!

Many thx for helping me

Björn

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
community guidelines
Most Valuable Participant ,
Feb 06, 2018 Feb 06, 2018

Copy link to clipboard

Copied

Sure. After this line:

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

Add this:

d.setDate(d.getDate()-1));

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
community guidelines
Explorer ,
Feb 06, 2018 Feb 06, 2018

Copy link to clipboard

Copied

Perfect !!!

Try, you are very smart!!!

Know I made a copy of that script and transformed it to another place with a Checkbox and the StartDate. The Checkbox gives me a value of 12 when it's True so it calculate with +12. Thats OK!

But the value in the field when it's False is not "" (nothing) It is 00.001.0000.

It must be also "" (nothing)...

Do you know why?

Kind Regards

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
community guidelines
Most Valuable Participant ,
Feb 06, 2018 Feb 06, 2018

Copy link to clipboard

Copied

When a check-box is not ticked its value is "Off". You need to add that as a condition in your code. If the value is "Off" then don't add anything to the 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
community guidelines
Explorer ,
Feb 06, 2018 Feb 06, 2018

Copy link to clipboard

Copied

Done

Perfect. thanks a lot...!

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
community guidelines
Explorer ,
Feb 07, 2018 Feb 07, 2018

Copy link to clipboard

Copied

Hi try,

unfortunately I got following result in my field again: "00.01.0000" when the Checkbox is "Off".

That might become to missunderstandings for other coleagues...

Here ist the JS:

var Datum_EinstellungString = this.getField("Datum_Einstellung").valueAsString; 

var BefrJ = Number(this.getField("BefrJ").valueAsString); 

if (Datum_EinstellungString=="" || BefrJ=="OFF") event.value = ""; 

else {     

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

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

    d.setDate(d.getDate()-1);

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

}

Is it possible that there are any mistakes...?

THX a lot for answering

BJ

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
community guidelines
Most Valuable Participant ,
Feb 07, 2018 Feb 07, 2018

Copy link to clipboard

Copied

JS is case-sensitive. It's "Off", not "OFF"...

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
community guidelines
Explorer ,
Feb 07, 2018 Feb 07, 2018

Copy link to clipboard

Copied

Same Problem:

var Datum_EinstellungString = this.getField("Datum_Einstellung").valueAsString; 

var BefrJ = Number(this.getField("BefrJ").valueAsString); 

if (Datum_EinstellungString=="" || BefrJ=="Off") event.value = ""; 

else {     

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

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

    d.setDate(d.getDate()-1);

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

}

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
community guidelines
Most Valuable Participant ,
Feb 07, 2018 Feb 07, 2018

Copy link to clipboard

Copied

Use this:

var Datum_EinstellungString = this.getField("Datum_Einstellung").valueAsString;

var BefrJ = this.getField("BefrJ").valueAsString;

if (Datum_EinstellungString=="" || BefrJ=="Off") event.value = "";

else {  

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

    d.setMonth(d.getMonth()+Number(BefrJ));

    d.setDate(d.getDate()-1);

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

}

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
community guidelines
Explorer ,
Feb 07, 2018 Feb 07, 2018

Copy link to clipboard

Copied

It works...!

you just have changes Number to the Addition...

unbelevable...!

THX!!!

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
community guidelines
Most Valuable Participant ,
Feb 07, 2018 Feb 07, 2018

Copy link to clipboard

Copied

I moved the Number constructor from line #2 because it was not working on a value like "Off".

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
community guidelines
New Here ,
May 04, 2018 May 04, 2018

Copy link to clipboard

Copied

I'm using this script in one of my PDFs, but I am having trouble with end of month issues. If the date is 1/31/2018, and I add one month, I want the end date to be 2/28/2018. Same for 5/31/2018. The end date should be 6/30/2018. Is there an easy way to accomplish 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
community guidelines
Most Valuable Participant ,
May 04, 2018 May 04, 2018

Copy link to clipboard

Copied

Not really... Here's what I would do:

Change the date to the first of the month, add ​7​ months to it, and then reduce one day from the 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
community guidelines
New Here ,
May 04, 2018 May 04, 2018

Copy link to clipboard

Copied

I've been searching a lot of different sites for an answer to this, and unfortunately I don't know enough JS. Would  there be a way to run the calculation, then have it look at both the start day (31) and the end day (3) to see if they're the same, and if they're not have it enter the last day of the previous month?

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
community guidelines
Most Valuable Participant ,
May 04, 2018 May 04, 2018

Copy link to clipboard

Copied

LATEST

There's no built-in command that gives you the last day of the month. Doing it the way I described is the best, unless you want to hard-code the number of days of each month into your script (and then you'll also need to take leap years into account...).

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
community guidelines
Most Valuable Participant ,
Feb 05, 2018 Feb 05, 2018

Copy link to clipboard

Copied

Instead of hoping it's better to plan in advance, so that it works correctly.

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
community guidelines
Explorer ,
Feb 05, 2018 Feb 05, 2018

Copy link to clipboard

Copied

I don't know the right Code for it.

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
community guidelines
LEGEND ,
Feb 05, 2018 Feb 05, 2018

Copy link to clipboard

Copied

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.

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
community guidelines