Skip to main content
Known Participant
May 19, 2021
Answered

Form Field Calculation: Subtracting 1 year from current date

  • May 19, 2021
  • 2 replies
  • 2303 views

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. 

 

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. 

This topic has been closed for replies.
Correct answer Nesa Nurani

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

 

 

2 replies

Thom Parker
Community Expert
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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
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?

 

Thom Parker
Community Expert
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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
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);}

 

 

CMunro87Author
Known Participant
May 21, 2021

Thank you!!