- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2021 02:26 AM
Hi all,
I have a requirement of calculating the age in years and months based on a date of birth field:
Age is a single line text variable and is read only. It should populate age based on date of birth in a format like : example : 5 years 4 months.
I am not knowledgable to glide date functions. Can someone help me with a script for this?
Thanks in advance!
Solved! Go to Solution.
- Labels:
-
Script Debugger
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 10:56 PM
Hi,
I don't find issue in my instance. Please share me the date you are passing and the output you get along with the alert details.
I removed some unwanted text in the code. You can use this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setVisible('certification_one',false);
g_form.hideFieldMsg('claim_dependency',true);
g_form.hideFieldMsg('insurance_plan',true);
}
var ageArr = newValue.split(" year");
var ageYear = 0;
if(ageArr.length > 1){
ageYear = parseInt(ageArr[0]);
alert('1 '+ageYear);
}
if(newValue != "21 years" && ageYear >= 21){
alert(ageYear);
g_form.setVisible('certification_one',true);
g_form.setMandatory('certification_one',true);
g_form.showFieldMsg('claim_dependency','Dependency ends at the 21st birthday unless your child is eligible for special allowance. See more information here.','info',true);
g_form.showFieldMsg('insurance_plan','A child can be enrolled in insurance up until the calendar year in which they turn 25 unless employed or married. Please confirm below.','info',true);
}
else{
g_form.setMandatory('certification_one',false);
g_form.setVisible('certification_one',false);
g_form.hideFieldMsg('claim_dependency',true);
g_form.hideFieldMsg('insurance_plan',true);
}
}
Palani

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 03:58 AM
Hi Anshi,
I tested this code in my instance. It is working fine. Can you tell me what data of births are you trying?
Thank you,
Palani
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 04:18 AM
Doesn't show up the field messages although more than 21 years:
1.
2.
Shows up:
It is only considering year. For example if we have 21 years and 2 months, it doesn't work in this condition.
Also , when I change the value in the dropdown, the message disappears

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 04:30 AM
Now got it. It is problem with the condition. we used > instead of >=. Replace this line in the script. Your issue will be resolved
if(ageYear >= 21){
Thank you,
Palani
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 04:34 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 05:44 AM
Hi
So, you need to show this message if DoB is more than 21 year. ie if it should be hidden if dob exactly 21 years or below 21 years.
In this case your condition should be:
if(newValue != "21 years" && ageYear >= 21){
Palani