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

Calculate date in 6 months minus 1 day

Community Beginner ,
Aug 03, 2018 Aug 03, 2018

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.

TOPICS
Acrobat SDK and JavaScript , Windows
1.7K
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 ,
Aug 03, 2018 Aug 03, 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 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 Beginner ,
Aug 03, 2018 Aug 03, 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=""}

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 ,
Aug 03, 2018 Aug 03, 2018

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

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 Beginner ,
Aug 03, 2018 Aug 03, 2018

Thom's script worked ok, automatically entered date upon opening of form but when I tried to add it to the other script I'd tried the field remains blank until you select a date, just like it worked before I started

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 ,
Aug 03, 2018 Aug 03, 2018

It's possible that a bad format is causing an exception to be thrown. Check the console window to see if any errors are being reported.

Also the value being set to the field is not the date. The variable name is wrong.

The solution is to fix the variable name and test the value of the starting date field before parsing.

var strDate this.getField("Date").valueAsString;

// Test for date format

if (/\d{1,2}\/\d{1,2}\/\d{2}/.test(strDate))

{

    var dt= util.scand("dd/mm/yy", strDate);

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

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

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

}

else

{event.value=""}

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 Beginner ,
Aug 03, 2018 Aug 03, 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

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
LEGEND ,
Aug 03, 2018 Aug 03, 2018

What happens when you add just 6 months to the dates of August 28, 2018, August 29, 2018, August 30, 2018 and August 31, 2018 what are you expecting the dates to be?

August 28, 2018 plus 6 months would be February 28, 2019. August 29, 2018 is the next day the date so for August 29, 2018 plus 6 months would be the next day or March 1, 2019.  And for August 30, 2018 the date would then be March 2, 2019, and for August 31, 2018 6 months later would be March 3, 2019. For the long months we need to account for the extra days some how.

Now if you want to reduce the result  by 1 day, you get the result of the JavaScript calculation.

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 ,
Aug 03, 2018 Aug 03, 2018

The unfortunate thing about dates is that months have different lengths, so you can't really add them evenly.  The setMonth function literally adds the number of months. So what do you do if the month day is greater in the starting month? One option is to just set it to the last day of that month, but they JS guys decided to bleed the difference in days over into the next month. Which is better/correct? Don't know. I just go with what it does.

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
LEGEND ,
Aug 04, 2018 Aug 04, 2018

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.

ExcelFormula.jpg

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 ,
Aug 04, 2018 Aug 04, 2018
LATEST

That's very interesting.  Excel has gone the other way.

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