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