Skip to main content
Known Participant
July 30, 2019
Question

EXACT Difference between two dates in months

  • July 30, 2019
  • 1 reply
  • 1229 views

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

}

})();

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
July 30, 2019

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

Known Participant
July 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?

Inspiring
July 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.