- 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 04:29 AM
Hi Ankur, This script is to be written in script include of client script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2021 04:33 AM
Hi,
you can use GlideAjax as the script requires date in Nov 25, 2000 format and not yyyy-MM-dd
Use onChange client script with Ajax and pass the date
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2021 04:41 AM
Hi Ankur, It's giving me javascript error
client script:
script include:
checkAge: function() {
var dob = this.getParameter(sysparam_id);
var dt = new GlideDate();
dt.setValue(dob);
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);
},
Please help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2021 05:10 AM
Hi,
why to use JSON parse?
Do this
checkAge: function() {
var dob = this.getParameter('sysparam_id'); // it should be in quotes
var dt = new GlideDate();
dt.setValue(dob);
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;
return years + ' years ' + months + ' months';
},
Update client script
alert(answer);
// remove the parsing line
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2021 05:17 AM
Hi, I removed the parsing line:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('HRAjaxUtils');
ga.addparam('sysparam_name','checkAge');
ga.addParam('sysparam_id',g_form.getValue('dob'));
ga.getXML(getData);
function getData(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
}
and updated the SI as well. Doesn't work