Calculate Employee age from Date of Birth

I have created a custom field called age on the employee form.

I want to display the age of an employee in the age field after the user inputs the date of birth and sets focus to another field. This is my script:

frappe.ui.form.on("Employee", "age", function(frm) {
  var dob = new Date(frm.doc.date_of_birth);
  var now = new Date();
  var age_now = now.getFullYear() - dob.getFullYear();

  cur_frm.set_value("age", age_now);
  cur_frm.refresh();
});

However, nothing happens to the age field.

Please assist.

This a sample test to calculate age I used in my work
JS code:

frappe.ui.form.on('DoctypeName', {
	refresh: function (frm) {
		
	},
	onload: function (frm) {
		if(frm.doc.dob){
			$(frm.fields_dict['age_html'].wrapper).html("AGE : " + get_age(frm.doc.dob));
		}
	}
});

frappe.ui.form.on("DoctypeName", "dob", function(frm) {
	if(frm.doc.dob){
		var today = new Date();
		var birthDate = new Date(frm.doc.dob);
		var age_str = get_age(frm.doc.dob);
		$(frm.fields_dict['age_html'].wrapper).html("AGE : " + age_str);
	}
});

var get_age = function (birth) {
	var ageMS = Date.parse(Date()) - Date.parse(birth);
	var age = new Date();
	age.setTime(ageMS);
	var years = age.getFullYear() - 1970;
	return years + " Year(s) " + age.getMonth() + " Month(s) " + age.getDate() + " Day(s)";
};

Python code:

def get_age(self):
		age_str = ""
		if self.dob:
			born = getdate(self.dob)
			age = dateutil.relativedelta.relativedelta(getdate(), born)
			age_str = str(age.years) + " year(s) " + str(age.months) + " month(s) " + str(age.days) + " day(s)"
		return age_str
  • dob is the name of the Date of Birth field
  • age_html is the name of the Age field with HTML fieldtype
6 Likes

@OmarJaber

Thank you very much. After tweaking it a little, I got it to work. A great script to start from.

1 Like

Hi @kevingee, do you mind to share how you do it?

1 Like

Possible share ur code

frappe.ui.form.on(‘Employee’, {
refresh: function(frm) {
var today = new Date();

    // this is how you get data from form
    var dateJoined = new Date(frm.doc.date_of_joining); 

    var duration = today.getFullYear() - dateJoined.getFullYear(); 
    var m = today.getMonth() - dateJoined.getMonth(); 
    if (m < 0 || (m === 0 && today.getDate() < dateJoined.getDate())) { 
      duration--; 
    } 
    // use frm.set_value to set value of a field
    frm.set_value('length_of_service', duration);
}

});

@Hardik_Gadesha

Hello my friend,
do you know how this working?
and where we can apply it? on client script?

@Nada-86

frappe.ui.form.on('Employee', {
	date_of_birth: function (frm) {
		if (frm.doc.date_of_birth) {
            var dob = frappe.datetime.str_to_obj(frm.doc.date_of_birth);
            var today = frappe.datetime.now_datetime();clearInterval
            var age = frappe.datetime.get_diff(today, dob);
            var ageInYears = Math.floor(age / 365);
            frm.set_value('age', ageInYears);
        }
	}
});

1 Like

Is there something missing? Didn’t work for me

@Nada-86

Always Check Basic Things,

  1. Your Doctype Name is Employee Age, Here given Code is for Doctype Employee, you need to make some changes here
  2. Check Field Name to Execute and Set value as per your doctype
2 Likes

@Hardik_Gadesha
Okay I will make the changes now.

@Hardik_Gadesha
Not working my friend.

Also here.

@Nada-86

Share Image of Employee Age Doctype

@Hardik_Gadesha


@Nada-86

Where is date_of_birth field ?

how will system knows compare to which date you want age ?

in ‘Employee’ DocType

@Nada-86 what is the reason behind creating this doctype ?

you can directly add Age field in Employee doctype to get all the employee age

1 Like

@Hardik_Gadesha
yeah I know but what I want is I can call all ages in other doctype.

Like if I will create a new doctype named ‘Players’ and I want to set a player name and his date of birth,
after that in doctype ‘Game’ I want to call player name and his age

@Nada-86 Fetch Date of Birth Field From Employee doctype to Employe Age Doctype and then use this script

1 Like

@Hardik_Gadesha
the script for which DocType?
Employee or Employee Age?

That what you mean fetch?