Skip to main content
Participant
March 22, 2023
Question

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

  • March 22, 2023
  • 3 replies
  • 1014 views

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?

3 replies

Legend
March 28, 2023

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.

Bernd Alheit
Community Expert
Community Expert
March 22, 2023

Look also at the day of month.

Participant
March 28, 2023

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

 

Bernd Alheit
Community Expert
Community Expert
March 28, 2023

When does you see 22 months and 28 days?

kglad
Community Expert
Community Expert
March 22, 2023

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>