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

Form Field Calculation: Subtracting 1 year from current date

Explorer ,
May 19, 2021 May 19, 2021

Im working on a form where I need a date to  auto populate every time the file is opened . I need it to populate the date 1 year ago. 

CMunro87_0-1621440118946.png

 

I have used the following scripts to populate the "WED" and "UED" fields with the current date:

 

function dateToday() {
var d = new Date();
f = this.getField("WED");
f.value = d;
}dateToday();

 

So now im looking for a way to populate the "WSD" and "USD" form fields with a date 1 year prior to the current date. 

 

Im new to this and self taught so please bear with me. 

TOPICS
Create PDFs , Edit and convert PDFs , How to , PDF forms
2.4K
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
1 ACCEPTED SOLUTION
Community Expert ,
May 19, 2021 May 19, 2021

For example to populate "WSD" field with one year less then "WED" field, in "WED" field as validation script use this:

if(event.value == "")
this.getField("WSD").value = "";
else{
var days = util.scand("mm/dd/yyyy", event.value)
days.setFullYear(days.getFullYear() -1);
this.getField("WSD").value = util.printd("mm/dd/yyyy", days);}

 

 

View solution in original post

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 19, 2021 May 19, 2021

For example to populate "WSD" field with one year less then "WED" field, in "WED" field as validation script use this:

if(event.value == "")
this.getField("WSD").value = "";
else{
var days = util.scand("mm/dd/yyyy", event.value)
days.setFullYear(days.getFullYear() -1);
this.getField("WSD").value = util.printd("mm/dd/yyyy", days);}

 

 

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 ,
May 21, 2021 May 21, 2021

Thank you!!

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 ,
Jul 17, 2023 Jul 17, 2023

Hi, I am attempting to learn JavaScript as I feel that I have a complex calculation involving dates. Please correct me if there is a way to do this without writing code.

 

In my PDF form, the employee will fill in a date that they need to begin using our gym facilities.  For example they need to start using the gym on September 1, 2023.  Since membership dues are paid one month in advance, I need the form to auto-populate the first day of the prior month.  Therefore for the September 1 example, I need the payment date field to auto-populate with August 1, 2023.

 

Can you advise how I can achieve 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 ,
Jul 17, 2023 Jul 17, 2023
LATEST

Please post this question to a new thread, since it is a new question. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 19, 2021 May 19, 2021

So here's script that will create a date 1 year from the current day

 

var today = new Date;
var oneYear = new Date((d.getMonth()+1).toString() +"/"+ d.getDate()+"/"+(d.getFullYear() + 1).toString());

this.getField("WED").value = oneYear;

 

And here is a simpler script for your current date .

 

this.getField("WED").value = new Date;


 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Enthusiast ,
May 21, 2021 May 21, 2021

Why is this marked as correct answer?

It's not what OP asked for, he wants one year prior to current date and not one year after current date, and field name should be "WSD" because "WED" is already populated with current date, and what is the point of variable today?

 

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 21, 2021 May 21, 2021
quote

Why is this marked as correct answer?

By @Asim123


You're correct in a way, however I'm not providing a custom solution. I'm pointing out how a script could be written to solve the issue. It's up to the user to fit it into thier project. I also don't see where it's marked as correct?

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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