Calculate date in 6 months minus 1 day

Community Beginner ,
Aug 03, 2018

Copy link to clipboard

Copied

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

Views

140

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Calculate date in 6 months minus 1 day

Community Beginner ,
Aug 03, 2018

Copy link to clipboard

Copied

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

Views

141

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 03, 2018 0
Adobe Community Professional ,
Aug 03, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 03, 2018 2
Community Beginner ,
Aug 03, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 03, 2018 0
Most Valuable Participant ,
Aug 03, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 03, 2018 0
Community Beginner ,
Aug 03, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 03, 2018 0
Adobe Community Professional ,
Aug 03, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 03, 2018 2
Community Beginner ,
Aug 03, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 03, 2018 0
Adobe Community Professional ,
Aug 03, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 03, 2018 0
Adobe Community Professional ,
Aug 03, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 03, 2018 0
Adobe Community Professional ,
Aug 04, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 04, 2018 0
Adobe Community Professional ,
Aug 04, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 04, 2018 0