Skip to main content
Participant
Aug 3, 2018 4:20 PM UTC
Question

Calculate date in 6 months minus 1 day

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 is closed to new replies. Start a new post to keep the conversation going.

10 replies

Thom Parker
Community Expert
Community Expert
Aug 3, 2018 4:39 PM UTC

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

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
Aug 3, 2018 5:52 PM UTC

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=""}

Translate
try67
Community Expert
Community Expert
Aug 3, 2018 7:05 PM UTC

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

Translate