Skip to main content
Known Participant
Aug 2, 2021 6:03 AM UTC
Answered

Count of age to convert to 1 year, when it's 6 motnhs

I am looking for a solution on how to update the script, so that it would give me a value of 1, when it is equal to 6 months. If the value is less than 6 months, to have 0. Also, I do not know how to make that the field would allow saving fixed value.

Here is the script I am using in 'Custom Calculation' box:

event.value = "";

var dobValue = getField("Textfield-49").value;

if (dobValue!="") {

var dob = util.scand("dd/mm/yyyy", dobValue);

var today = new Date();

var age = today.getTime() - dob.getTime();

// compute age in milliseconds

var nAgeMilliseconds = today.getTime() - dob.getTime();

var nAgeYears = ( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

event.value = Math.floor(nAgeYears);

}

 

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Aug 4, 2021 1:18 PM UTC

03/02/2021 (dd/mm/yyyy)


OK, I see what you mean. That's because the age calculation in the script you used was not accurate (by trying to be very accurate, ironically).

Change this line:

var nAgeYears = ( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

 

To this:

var nAgeYears = age / (1000 * 60 * 60 * 24 * 365);

9 replies

try67
Community Expert
Community Expert
Aug 2, 2021 12:38 PM UTC

Change this line:

event.value = Math.floor(nAgeYears);

To this:

event.value = (nAgeYears%1)>=0.5 ? Math.ceil(nAgeYears) : Math.floor(nAgeYears);

Translate
Beata5CBFAuthor
Known Participant
Aug 3, 2021 4:59 AM UTC

Hello @try67 ,

Thank you for your reply, however, the provided solution of replacement works the same way, and there is no possibility to fix the value and save it.

Is there any other possibility?

Translate
try67
Community Expert
Community Expert
Aug 3, 2021 12:52 PM UTC

I'm sorry, I don't understand what you mean...

Translate