I have a requirement of calculating the age in years and months based on a date of birth field:

Deepanshi Sood
Giga Expert

Hi all,
@Ankur Bawiskar 
@palanikumar 
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.

find_real_file.png

I am not knowledgable to glide date functions. Can someone help me with a script for this?

Thanks in advance!

1 ACCEPTED SOLUTION

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

Thank you,
Palani

View solution in original post

38 REPLIES 38

Jaspal Singh
Mega Patron
Mega Patron

Hi Anshi,

 

There already exists a solution for this in one of other thread. Refer link for a check & update it with the variables you using accordingly.

Hi @Jaspal Singh This doesn't give me the age in the format that I need.
I need it in '7 years 5 months' (example)

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

can you check this link;

this might help you to do in your format

1) get the difference and then use this

https://stackoverflow.com/questions/38355157/is-there-a-method-to-convert-miliseconds-to-years-month...

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Anshi 

Please check the below sample script which worked for me

1) Pass the date from client to server in ajax and then do that

Script: Date of Birth 25th Nov 2000

var date = '2000-11-25';

var dt = new GlideDate();
dt.setValue(date);

var newFormat = dt.getByFormat("MMM dd, yyyy");

var diff = Date.now() - Date.parse(newFormat);

var seconds = Math.floor(diff/1000);
var minutes = Math.floor(seconds/60);
var hours   = Math.floor(minutes/60);
var days    = Math.floor(hours/24);
var months  = Math.floor(days/30);
var years   = Math.floor(days/365);

seconds %= 60;
minutes %= 60;
hours %= 24;
days %= 30;
months %= 12;

gs.info("Years->" + years);
gs.info("Months->" + months);

Output:

find_real_file.png

Actual: Compare with online tool

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader