Skip to main content
Known Participant
August 2, 2021
Answered

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

  • August 2, 2021
  • 1 reply
  • 978 views

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 has been closed for replies.
Correct answer try67

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);

1 reply

try67
Community Expert
Community Expert
August 2, 2021

Change this line:

event.value = Math.floor(nAgeYears);

To this:

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

Beata5CBFAuthor
Known Participant
August 3, 2021

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?

try67
Community Expert
Community Expert
August 3, 2021

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