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

Hi,

So are you in HR scope?

Please share complete script include script and screenshots

Regards
Ankur

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

yes,

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;

return years + ' years ' + months + ' months';
},

Hi @Anshi,

I suggest you not to use this script as this is not accurate. This script considers one months as 30 days. As per this logic a year is 360 days. If you check for a date which is exactly 1 year ago, it will give return 1 Year and 5 days. For every 1 Year it will have add 5 more days.

Try the one I shared. It may look complex, but I don't find any other option.

Thank you,

Palani

Thank you,
Palani

palanikumar
Mega Sage

Hi Anshi,

I didn't find any option to get accurate Age. So created a customs script. You can try this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var age = calculateAge(newValue);
    g_form.setValue("age",age); // update age with actual field / variable
}

function calculateAge(dobString) {
    var dob = new Date(dobString);
    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;
}
Thank you,
Palani

Hi Palani, Thanks for responding. I tried this script but it doesn't work as required.
I want the age in years and months only not days. It is not populating in some cases:

for example: when I am selecting:
find_real_file.png

In this case it should show the months - 1 month for the above case

 

find_real_file.png

In this case it is not showing up any value