How do I calculate # of months between 2 dates in Acrobat DC form?

Nov 03, 2017

Copy link to clipboard

Copied

Hi there,

Admitted JavaScript idiot here.  Need to calculate number of months to 2 decimals between a move in and move out date.

I got it down to the number of days using what's below.

To make it easy, I only need to take the result from this script and divide by 30.

I do not need years.  So a calculation of more than 12 months is fine.

Could someone help??

Thank you!!

// Custom calculate script

(function () {

var sStart = getField("MOVE_IN_DATE_CLIENT").valueAsString;

var sEnd = getField("MOVE_OUT_DATE_CLIENT").valueAsString;

var dStart, dEnd, diff;

if(sStart && sEnd) {

dStart = util.scand("mm/dd/yy", sStart);

dEnd = util.scand("mm/dd/yy", sEnd);

diff = dEnd.getTime() - dStart.getTime();

event.value = Math.floor(diff / 864e5);

} else {

event.value = "";

}

})();

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Change:

event.value = Math.floor(diff / 864e5);

To:

event.value = Math.floor(diff / 864e5) / 30;

TOPICS
Acrobat SDK and JavaScript

Views

593

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

How do I calculate # of months between 2 dates in Acrobat DC form?

Nov 03, 2017

Copy link to clipboard

Copied

Hi there,

Admitted JavaScript idiot here.  Need to calculate number of months to 2 decimals between a move in and move out date.

I got it down to the number of days using what's below.

To make it easy, I only need to take the result from this script and divide by 30.

I do not need years.  So a calculation of more than 12 months is fine.

Could someone help??

Thank you!!

// Custom calculate script

(function () {

var sStart = getField("MOVE_IN_DATE_CLIENT").valueAsString;

var sEnd = getField("MOVE_OUT_DATE_CLIENT").valueAsString;

var dStart, dEnd, diff;

if(sStart && sEnd) {

dStart = util.scand("mm/dd/yy", sStart);

dEnd = util.scand("mm/dd/yy", sEnd);

diff = dEnd.getTime() - dStart.getTime();

event.value = Math.floor(diff / 864e5);

} else {

event.value = "";

}

})();

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Change:

event.value = Math.floor(diff / 864e5);

To:

event.value = Math.floor(diff / 864e5) / 30;

TOPICS
Acrobat SDK and JavaScript

Views

594

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
Nov 03, 2017 0
Most Valuable Participant ,
Nov 03, 2017

Copy link to clipboard

Copied

Just divide the result by 12...

On Fri, Nov 3, 2017 at 4:47 PM, scotts74312160 <forums_noreply@adobe.com>

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...
Nov 03, 2017 1
Most Valuable Participant ,
Nov 03, 2017

Copy link to clipboard

Copied

Sorry, I meant by 30...

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...
Nov 03, 2017 1
Adobe Community Professional ,
Nov 03, 2017

Copy link to clipboard

Copied

One can use the getFullYear(), getMoth() and getDate() methods to get the number of years,  months and the date within the month respectively. With these values one can easily calculate the number or fractional months to use in the unused term calculation even when the dates cross a year boundary and the all months are assumed to have 30 days.

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...
Nov 03, 2017 1
Nov 03, 2017

Copy link to clipboard

Copied

I'm ok with assuming a 30 day month for my purposes.  The calculation is general for what we need it for.  How would I divide this result by 30 using my existing script?

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...
Nov 03, 2017 0
Most Valuable Participant ,
Nov 03, 2017

Copy link to clipboard

Copied

Change:

event.value = Math.floor(diff / 864e5);

To:

event.value = Math.floor(diff / 864e5) / 30;

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...
Nov 03, 2017 2
Nov 06, 2017

Copy link to clipboard

Copied

Thank You!

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...
Nov 06, 2017 0