- 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-23-2021 05:31 AM
Hi Anshi,
Its issue with date format. Javascript is not accepting the date format in your system. I updated the script to convert the date to format I see in your screenshot. I don't find option to dynamically pick the date format. Hope the Date format is always constant in your environment:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var age = calculateAge(newValue);
g_form.setValue("u_age", age);
}
function calculateAge(dobString) {
var dateArray = dobString.toString().split('-');
var dYear = dateArray[2];
var dMonth = dateArray[1];
var dDay = dateArray[0];
var dob = new Date(dYear + "/" + dMonth + "/" + dDay);
var today = new Date();
if (today <= dob) {
return "Invalid Date of Birth";
}
var yearDob = dob.getYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate();
var yearNow = today.getYear();
var monthNow = today.getMonth();
var dateNow = today.getDate();
var age = {};
var ageArray = [];
var ageString = "";
var yearString = "";
var monthString = "";
var dayString = "";
var yearAge = yearNow - yearDob;
var monthAge = monthNow - monthDob;
if (monthNow < monthDob) {
yearAge--;
monthAge = 12 + monthNow - monthDob;
}
var dateAge = dateNow - dateDob;
if (dateNow < dateDob) {
monthAge--;
dateAge = 31 + dateNow - dateDob;
if (monthAge < 0) {
monthAge = 11;
yearAge--;
}
}
age = {
years: yearAge,
months: monthAge,
days: dateAge
};
if (age.years > 1) yearString = " years";
else yearString = " year";
if (age.months > 1) monthString = " months";
else monthString = " month";
if (age.days > 1) dayString = " days";
else dayString = " day";
if (age.years > 0) {
ageArray.push(age.years + yearString);
}
if (age.months > 0) {
ageArray.push(age.months + monthString);
}
if (age.days > 0) {
ageArray.push(age.days + dayString);
}
ageString = ageArray.join(" ");
return ageString;
}
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 12:11 AM
Hi
I tried the below client script but it is not working correctly , It is showing up for wrong age also and disappears as I select any other value in the dropdown:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setVisible('certification_one',false);
g_form.hideFieldMsg('claim_dependency','Dependency ends at the 21st birthday unless your child is eligible for special allowance. See more information here.','info',false);
g_form.hideFieldMsg('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',false);
//return;
}
if(newValue > '21 years'){
g_form.setVisible('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);
}
}
Please help! Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 12:35 AM
Hi Anshi,
Since the age is a String, this condition will not work. You need to first split the years then only you can check. I think you need a else condition to remove the message. I converted the age and updated the condition. You can use this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setVisible('certification_one',false);
g_form.hideFieldMsg('claim_dependency','Dependency ends at the 21st birthday unless your child is eligible for special allowance. See more information here.','info',false);
g_form.hideFieldMsg('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',false);
//return;
}
var ageArr = newValue.split(" year");
var ageYear = 0;
if(ageArr.length > 1){
ageYear = parseInt(ageArr[0]);
}
if(ageYear >= 21){
g_form.setVisible('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);
}
}
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 12:48 AM
Hi, It is showing up for less than 20 years as well. If I am selecting less than 21 years for the first time the messages are not showing up but if I am selecting anything more than 21 years and then less than 21 years, the message still remains.
also the condition should be >21
if(ageYear > 21)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2021 12:55 AM
Hi Anshi,
I mentioned in previous message to add else part :). You need to add else part like highlighted below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setVisible('certification_one',false);
g_form.hideFieldMsg('claim_dependency','Dependency ends at the 21st birthday unless your child is eligible for special allowance. See more information here.','info',false);
g_form.hideFieldMsg('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',false);
//return;
}
var ageArr = newValue.split(" year");
var ageYear = 0;
if(ageArr.length > 1){
ageYear = parseInt(ageArr[0]);
}
if(ageYear >= 21){
g_form.setVisible('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.setVisible('certification_one',false);
g_form.hideFieldMsg('claim_dependency',true);
g_form.hideFieldMsg('insurance_plan',true);
}
}
Palani