Skip to main content
Participant
Mar 22, 2023 1:48 AM UTC
Question

Create a custom calculation script for age in months that doesn't round up

I am trying to fix a PDF form to calculate a child's age in months.  I have been trying to use the below custom calculation script

var cDateFormat = "yyyy-mm-dd";

var cDate = this.getField("dob").value;

if (cDate != "") { var oDate = util.scand(cDateFormat, cDate); }

if (oDate == null) app.alert("Invalid date: " + cDate, 1, 0);

else {

var now = new Date();

var nMonth = Number(now.getMonth());

var nYear = Number(now.getFullYear());

var diffYear = nYear - oDate.getFullYear();

var diffMonth = nMonth-oDate.getMonth();

if (diffYear) { diffYear = diffYear * 12; }

event.value = diffMonth + diffYear;

}

 

How do I change this formula so that I don't have the month rounding up? This current formula will make 22 months 28 days into 23 months, and I need it to stay at 22 months?

Is there a way to fix this formula to eliminate the rounding up to the next month?

6 replies

Participant
Nov 13, 2026 8:42 AM UTC

The issue is that your current formula calculates the difference in years and months but doesn't check the day of the month. That's why it shows 23 months even when the child is only 22 months and 28 days old.

You need to subtract one month whenever the current day is earlier than the birth day.

Try replacing your calculation with this:

var cDateFormat = "yyyy-mm-dd";
var cDate = this.getField("dob").value;

if (cDate == "") {
event.value = "";
} else {
var oDate = util.scand(cDateFormat, cDate);

if (oDate == null) {
app.alert("Please enter a valid date of birth.");
event.value = "";
} else {
var now = new Date();

var totalMonths =
(now.getFullYear() - oDate.getFullYear()) * 12 +
(now.getMonth() - oDate.getMonth());

// Subtract one month if the current day hasn't reached
// the birth day yet.
if (now.getDate() < oDate.getDate()) {
totalMonths--;
}

event.value = Math.max(0, totalMonths);
}
}

For example, if the birth date is January 15 and today's date is November 13 of the following year, the result will be 21 completed months, not 22.

This calculates completed calendar months rather than rounding up partial months.

One edge case to consider: if the birth date is January 31 and the current date is February 28, this strict calculation returns 0 completed months. You may want to decide whether the last day of February should count as one completed month in your form.

For testing your expected age calculations against different birth dates, you can also compare the results with this <a href="https://hisaabage.com/age-calculator/">online age calculator</a>, which displays age in years, months and days.

Hope this helps!

Translate
Legend
Mar 28, 2023 12:20 PM UTC

This isn't an issue of rounding at all. Your code has to do what YOU would do in giving the child's age.  For example. The child was born in January. It is 20 July. How old are they? Can YOU answer that question?  Or do you need more info? And, if you need more info, what EXACTLY do you do with it?  The code has to match what a human would do.

Translate
Bernd Alheit
Community Expert
Community Expert
Mar 22, 2023 12:37 PM UTC

Look also at the day of month.

Translate
Participant
Mar 28, 2023 11:39 AM UTC

I honestly have no idea how to do that.  Would you be able to assist me?

 

Translate
Bernd Alheit
Community Expert
Community Expert
Mar 28, 2023 2:43 PM UTC

When does you see 22 months and 28 days?

Translate
kglad
Community Expert
Community Expert
Mar 22, 2023 1:51 AM UTC

in the future, to find the best place to post your message, use the list here, https://community.adobe.com/

 

p.s. i don't think the adobe website, and forums in particular, are easy to navigate, so don't spend a lot of time searching that forum list. do your best and we'll move the post if it helps you get responses.

 

<moved from using the community>

Translate