Calculate Age for a Student

@James_Robertson use the “Read Only” fieldtype! it’s not stored in database, and you can customize they value using javascript

In addition to @max_morais_dmm’s answer, you can use Moment.js library for calculating the age of the Student or can refer the datetime.js file in the frappe.

Thanks @max_morais_dmm and @ManasSolanki. That makes sense. So I am not a programmer type and don’t know javascript. Assuming I can figure that out, where on the doctype form do I place said code to have the field get calculated?

I essentially want to take the out of box date_of_birth field and divide it by 365.25 to give me the age in years as a whole number/integer.

Thoughts?

The date_of_birth field is of type ‘date’.
You can use:
var now = moment();
var total_days = moment(now).diff(date, “days”);
var number_of_years = Math.floor((total_days).toFixed(2) / 365.25);

Please help me calculate “age” of just about anything that’s given in a “date” field :smiley: please please :smiley:

@iMoshi

cur_frm.set_value(AgeField, moment().diff(cur_frm.doc.DATEFIELDNAME, 'years'));
cur_frm.refresh_field(AgeField)
2 Likes

OMG, thank you so much! Just had to add semicolons and apostrophes :smiley: you are a life saver!

However, this code is not working on child tables. Any help on that…? :smiley:

Thank you again!

1 Like

@iMoshi

For child Table,
cur_frm.doc.CHILDTABLE_FIELD_NAME[index].AGEFIELD=moment().diff(cur_frm.doc.DATEFIELDNAME, ‘years’)
cur_frm.refresh_fields(“CHILDTABLE_FIELD_NAME”)

2 Likes

You can create your custom fields(i.e read only and other fields) using customize form , have to write custom script to calculate the age (@ROHAN_JAIN1)has given . Export custom fields and custom script to app using fixtures. If you update doctypes your custom changes will be earsed , by using fixtures you can pull from repository to reflect your changes in your current system

Man, just can’t get it to work… Is hitting “Add script for Child Table” the right thing?

I have “birthday” date field and would like to have “Age” (child_age) read-only field to calculate Please take a look at the screen shot :smiley:

Thank you so much!

1 Like

Man, “I’m all in” deep. Got the dev mode on, did lots of changes to many DocTypes… Can’t go back :stuck_out_tongue_winking_eye:

I’ve learned to not mess with the standard DocTypes, but we have really specific differences in my country.

Can you please share screenshot of child doctype too?

:slight_smile:

Here you go :smiley:

@iMoshi
frappe.ui.form.on(“Employee Children”, {
birthday:function (frm,cdt,cdn) {
locals[cdt][cdn].age=moment().diff(locals[cdt][cdn].birthday(), ‘years’);
cur_frm.refresh_fields(“ChildTableName”)
}
});

2 Likes

I somehow can’t get it to work. Fiddled with it a lot, no go :frowning: ERPNext throws bunch of JS error no matter what I do… I’m just going to hide it for now.

Thank you so much Rohan!

I came across another question.

Can we calculate “duration” of a “start date” and “end date” in a child table? :smiley:

Any help is appreciated.

Thanks guys! :heart:

Yes we Can calculate it.

2 Likes

I should start paying you :stuck_out_tongue_winking_eye:

Thank you again Rohan!

Thank you so much for this screenshot. It worked for me!