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

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

Explorer ,
Feb 05, 2018 Feb 05, 2018

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
3.5K
Translate
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 , 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

...
Translate
Community Expert ,
Feb 05, 2018 Feb 05, 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?

Translate
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

THX

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

Translate
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

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.

Translate
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

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

Translate
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 ,
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.yyyy", startDateString);

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

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

}

Translate
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

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

Translate
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 ,
Feb 06, 2018 Feb 06, 2018

Sure. After this line:

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

Add this:

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

Translate
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

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

Translate
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 ,
Feb 06, 2018 Feb 06, 2018

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.

Translate
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

Done

Perfect. thanks a lot...!

Translate
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

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

Translate
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 ,
Feb 07, 2018 Feb 07, 2018

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

Translate
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

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

}

Translate
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 ,
Feb 07, 2018 Feb 07, 2018

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

}

Translate
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

It works...!

you just have changes Number to the Addition...

unbelevable...!

THX!!!

Translate
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 ,
Feb 07, 2018 Feb 07, 2018

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

Translate
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

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?

Translate
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 ,
May 04, 2018 May 04, 2018

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.

Translate
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

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?

Translate
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 ,
May 04, 2018 May 04, 2018
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...).

Translate
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 ,
Feb 05, 2018 Feb 05, 2018

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

Translate
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

I don't know the right Code for it.

Translate
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

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.

Translate
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