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

EXACT Difference between two dates in months

New Here ,
Jul 30, 2019 Jul 30, 2019

HI! I have a formula that is calculating the difference between two dates and converting it into a decimal, however it is calculating off a 30 day month and I want it to be exact. For example DOB-06/02/17, Date6 is 7/29/19, the calculation I am getting is 26,23, when it should be 25.94. There are 788 days between the two dates, but I need to know the difference in months. Below is my calculation-can anyone help? If I can't make this happen, can I override the calculation and manually enter the correct info? Thank you!!!

(function () {

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

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

var dStart, dEnd, diff;

if(sStart && sEnd) {

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

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

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

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

} else {

event.value = "";

}

})();

TOPICS
PDF forms
1.2K
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 ,
Jul 30, 2019 Jul 30, 2019

How did you come to the result of 25.94? What's the logic behind it?

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
New Here ,
Jul 31, 2019 Jul 31, 2019

Try, the logic is if a child is born on 6/2/17, they wouldn't be 26 months until 8/2/19, then 28 of 31 is actually 90.32. If it isn't possible to get the formula form month to month, how can I change this to custom validation script that would allow me to override it?

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 ,
Jul 31, 2019 Jul 31, 2019

There are other methods available to the date object beside "getTime". I would look at using the "getFullyear", "getMonth", and "getDate" methods to compute the full number of years, months, and the number of the day within the month. You then only need to get the number of days in the birth month and you should have all the information for your computation.

It would not hurt to add some comments to your code to explain what it is doing.

Unless you are a banker using bank formulas a month does not always have 30 days.

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
New Here ,
Aug 06, 2019 Aug 06, 2019
LATEST

Ok, I have been trying to figure out how to write that code, but I am no idea! If you could give me an example or point me to one, that would be great!

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