Skip to main content
Participant
August 3, 2018
質問

Calculate date in 6 months minus 1 day

  • August 3, 2018
  • 返信数 1.
  • 1845 ビュー

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.

このトピックへの返信は締め切られました。

返信数 1

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

Inspiring
August 4, 2018

That worked great, thanks.

It doesn't like 30th & 31st of Aug as displays as 1st & 2nd Mar in "next", but I can live with that.

Thanks so much for your help


Using Excel to do this computation, one gets a different answer when using the EDATE function to add months and subtract 1 day than when using the Date function to add 6 months and subtract one day.