Skip to main content
Participant
August 3, 2018
Question

Calculate date in 6 months minus 1 day

  • August 3, 2018
  • 1 reply
  • 1840 views

I've created an inspection form for work and would like it to auto calculate the date for next inspection 6 months minus 1 day from the date carried out. I found a script for adding days and that works fine between e.g Aug to Feb but not for Sep to Mar, where Feb only has 28 days. Any suggestions would be appreciated.

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
August 3, 2018

Here's a script that explicitly adds 6 months and then subtracts 1 day from the current date.

This script is placed in the calculate script for the field where the extended date is shown.

var dt = new Date;

dt.setMonth(dt.getMonth()+6);

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

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

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
August 3, 2018

Thanks. Now say I only wanted it to show once I've added an entry into the "Date" field and then it add on from that?

I tried to add it into the script I'd found before but it doesn't display anything (I really have no clue what I'm doing lol)

var dt= util.scand("dd/mm/yy", this.getField("Date").value);

dt.setMonth(dt.getMonth()+6);

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

if (this.getField("Date").value!="")

{

event.value=util.printd("dd/mm/yy",date)

}

else

{event.value=""}

try67
Community Expert
Community Expert
August 3, 2018

That should work... If it doesn't, what happens when you use it, exactly?